Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner

So, I'm getting stuck with this piece of code:

import java.util.InputMismatchException;
import java.util.Scanner;

public class ConsoleReader {

    Scanner reader;

    public ConsoleReader() {
        reader = new Scanner(System.in);
        //reader.useDelimiter(System.getProperty("line.separator"));
    }

    public int readInt(String msg) {
        int num = 0;
        boolean loop = true;

        while (loop) {
            try {
                System.out.println(msg);
                num = reader.nextInt();

                loop = false;
            } catch (InputMismatchException e) {
                System.out.println("Invalid value!");
            } 
        }
        return num;
    }
}

and here is my output:

Insert a integer number:
Invalid value!
Insert a integer number:
Invalid value!
...

like image 286
mateusmaso Avatar asked Aug 26 '10 04:08

mateusmaso


People also ask

How do I stop 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.

What is Java InputMismatchException?

The InputMismatchException occur when the user does not provide the proper type of input or input is out of range. In simple words, we get the InputMismatchException when the input type is not correct.


1 Answers

As per the javadoc for Scanner:

When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

That means that if the next token is not an int, it throws the InputMismatchException, but the token stays there. So on the next iteration of the loop, reader.nextInt() reads the same token again and throws the exception again. What you need is to use it up. Add a reader.next() inside your catch to consume the token, which is invalid and needs to be discarded.

...
} catch (InputMismatchException e) {
    System.out.println("Invalid value!");
    reader.next(); // this consumes the invalid token
} 
like image 56
samitgaur Avatar answered Oct 03 '22 09:10

samitgaur