• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting program to nto crash but instead give error message for certain input  RSS feed

 
Kathy Cruz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want my program to prompt user to enter radius, and whether they want to calculate area, or circumference, or diameter. So far, my program works, but I do not want it to crash when user inputs a a word instead of a numerical value or D, C, or A (these capital letters mean Diameter, Area, or Circumference). Could someone please help me in getting my program to not crash, but instead give the user an error message??

My code so far is this:

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would bet the program is throwing an exception. One thing you could do is catch that exception and print the error message there.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Scanner class has methods to check to see if the input is in a particular form so you could try using one of the hasXXX methods to see if the input is a double.
Then, assuming it is a double, you probably also want to do some bounds checking on the input value to make sure the value is greater than 0.
Finally, on your second input, you may want a simple 'else' clause to catch any illegal letter input. BTW why are you forcing the user to use upper case letters why not use the equalsIgnoreCase() method rather than the equals() method and you may want to use trim() to remove any spurious white spaces from the input.
 
Daryl Cofer
Greenhorn
Posts: 19
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also try to surround the problematic code with a try/catch. This will execute any of the code within the try block and if an exception is thrown: catch it, print a message and continue executing the code after the catch block is completed.

Links:
Try block
Catch block

Hope this helps!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daryl Cofer wrote:You could also try to surround the problematic code with a try/catch.

Yes you can do this but I wouldn't recommend it given there is an easy way to check if the value is a valid double.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: sorry to come late to this, but Tony is correct. If you have problems with Scanner, Rob Spoor pointed out ages ago that it is always possible to avoid such Exceptions. Search my posts for “utility class”, and you will find how to write a utility class to use Scanners, and how to avoid such Exceptions. You won't find everything in one post, however.
You know about the problem with nextLine()? You will find it explained in my post in this thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!