aspose file tools*
The moose likes Beginning Java and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Gary Lineker
Greenhorn

Joined: Sep 20, 2012
Posts: 2
[Hello guys,

Java noob here so would really appreciate a little help. The program does exactly what it's supposed to do, except when the user enters the wrong variable type for the score.

It's a simple conversion program: user enters serial # of student, enters the score out of 100, and the program converts the score to a grade, i.e., 92 = a grade of A.

The issue is when the user enters a string rather than an integer; the following error message occurs:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at letterGrade.main(letterGrade.java:20)

I need a way to tell the user that if he/she doesn't enter an integer for the score, the program should print as much.

Java code below. Thank you so much:


Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
You could use the scanners hasNextInt() method to check to see if the input is an integer before calling attempting to call nextInt().
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Your code should be something like:



So, for example, you could construct a loop that parses the input and then sets a flag after successful parse, but in the case of a failure catches the exception, prints out a message to the user. The loop then repeats by virtue of the fact that you didn't set the "yes, we succeeded" flag.

Or, you could start with the flag being set to successful and then only explicitly set it to failure when you catch the exception. Six of one, half a dozen of the other.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Nice to see you again, Jeff

Welcome (again).
If you use Scanner (I know some people dislike Scanner; I don’t, but look here for a nasty potential pitfall), you can avoid both parsing and that Exception with the hasNextInt() method that Tony Docherty mentioned. What I won’t tell you is that I have written about that before, and you can find those posts by searching my posts for hasNextInt and Scanner. You can put that all in a utility class (which I recommend). I haven’t written about utility classes for ages, well over 13 minutes, and you can find some links here. You might even find a link to hasNextInt() in that post!

I have edited your post to take the bold print out; it is unnecessary. What is necessary, however, is the code button. When I edited your post I used that, and you can see how much better the code looks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
By the way: you don’t need nested if-elses. You can do all the grade checking, including error messages for entries of 123 or -99, in one set of if-elses.
 
wood burning stoves
 
subject: Exceptions