• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tip Calculator Trying to Catch an error  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi this is my second attempt to write a simple program and I am trying to teach myself Java. I am studying software engineering but I don't have a java class for almost a year from now. My problem is I have tried to create a way to catch and display an error message if the user inputs anything other than an integer when asked what percent to tip the server... I have been watching Derek Banas on you tube and in an early lesson he did this creating a method.... I have attempted to do this but when I enter a double instead of an integer I get a Input Mismatch error.. The rest of the program seems to run fine but I am trying to create ways to catch errors from the user input... This is my first post so I apologize in advance if I have made this post wrong by anyway. Any help would be greatly appreciated.. Thanks Scott

 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

You can at first attempt to convert user's input into Integer :
Integer.parseInt(userNumber)
Then you can surround this line in try catch block which ensures that exception is caught
 
Scott Wells
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your very quick reply. I will give it a try and let you know how it works out.

Scott Wells
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!