This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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


Gary Lineker

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(
at java.util.Scanner.nextInt(
at java.util.Scanner.nextInt(
at letterGrade.main(

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

Joined: Aug 07, 2007
Posts: 2724
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

Joined: Jan 03, 2004
Posts: 6109

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

Joined: Oct 13, 2005
Posts: 43892
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

Joined: Oct 13, 2005
Posts: 43892
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.
I agree. Here's the link:
subject: Exceptions
It's not a secret anymore!