This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help with exception

 
boki markos
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically i need to ask the user if they wanna change dimensions of silo 1 and then silo 2.. but i want to put in an exception to catch any numbers, or other characters that arent letters... which exception would i use?

[edit]Add some new lines so as to fit all code into width of screen. CR.[/edit]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have already initialised num to -1? In which case NumberFormatException looks correct.

But those multiple if-elses don't. Imagine how complicated that would look if you had a silo3 or a silo4. Is there any way of passing a reference to the silo to change, so you can call

s.setHeight(num);

or

s.setRadius(num);

I am not happy to see a System.exit() call. That is probably safe in this instance, but if you have a larger application, you might interfere with other processes going on, and have something disastrous like a file half-written and not properly closed.
There are other dialogs available in JOptionPane; there is one which can return YES/NO, which would probably fit your purposes better than input dialog for getting YES/NO.
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to consider a numeric text field or a JSpinner
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic