Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java InputMismatchException

I have this code and I want to catch the letter exception but it keeps having these errors:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at exercise_one.Exercise.main(Exercise.java:17)

And here is my code:

 System.out.print("Enter the number of students: ");

 students = input.nextInt(); 

 while (students <= 0) {

     try {

        System.out.print("Enter the number of students: ");

        students = input.nextInt();

     }

     catch (InputMismatchException e) {

        System.out.print("Enter the number of students");

     }
 }    
like image 611
John Stef Avatar asked May 29 '13 14:05

John Stef


People also ask

What is InputMismatchException in Java?

java.util.InputMismatchException. Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type. See also: Scanner.

How do I fix Java Util InputMismatchException?

The only way to handle this exception is to make sure that you enter proper values while passing inputs. It is suggested to specify required values with complete details while reading data from user using scanner class.

How do you handle input exception in Java?

You should change the name reading to scanner. nextLine() and if you want to handle invalid int inputs from the user, the call to nextInt needs to be in your try block and catch the InputMismatchException instead of the ClassCastException.

What is the difference between NumberFormatException and InputMismatchException?

InputMismatchException is specific for the Scanner . It indicates invalid type, not necessarily an invalid number. NumberFormatException is specific for trying to convert a non numeric string to a number.


2 Answers

You can use a do-while loop instead to eliminate the first input.nextInt().

int students = 0;
do {
    try {
        // Get input 
        System.out.print("Enter the number of students: ");
        students = input.nextInt();
    } catch (InputMismatchException e) {
        System.out.print("Invalid number of students. ");
    }
    input.nextLine(); // clears the buffer
} while (students <= 0);

// Do something with guaranteed valid value 

Therefore all InputMismatchException can be handled in one place.

like image 129
Siyu Song Avatar answered Nov 04 '22 02:11

Siyu Song


from the doc

Scanner.nextInt Scans the next token of the input as an int. if the next token does not match the Integer regular expression, or is out of range

So it seems you are not entering any integer as input.

you can use

     while (students <= 0) {

         try {
            System.out.print("Enter the number of students: ");

            students = input1.nextInt();

         }

         catch (InputMismatchException e) {
             input1.nextLine();
         }
     } 
like image 24
stinepike Avatar answered Nov 04 '22 03:11

stinepike