'While loop with try/catch causes endless loop

The code snippet below causes an endless loop, when I enter a character. That's really weird for me, for a wrong number it works fine, but when I am entering any character, it causes an endless loop.

boolean checkValue = false;

Scanner console = new Scanner(System.in);
    while (!checkValue) {

System.out.println("Enter 1, 2 or 3");      

    try {
            input = console.nextInt();

       switch (input) {
            case 1:
                // code 
                checkValue = true;
                break;
            case 2:
                 // code 
                checkValue = true;
                break;
            case 3:
               // code 
                checkValue =  true;
                break;

            default: // when entered a wrong number
                System.err.println("Wrong Input"); 
                checkValue = false;
        }
        }

            catch(Exception e) { // when entered a character

                System.err.println("Wrong Input"); 
                checkValue = false;

            } 
   }    


Solution 1:[1]

You assign the input into one variable :

        input = console.nextInt();

But the switch statement tests a different variable :

   switch (auswahl) {

EDIT :

Add console.nextLine() to your exception handler, to consume the end of the current line, to allow the next nextInt to read from the next line.

Solution 2:[2]

What's "auswha1" ? Its value is not updated and your scanner is outside the loop. Moreover, are you sure that you are testing a character in your switch ? It looks like an int.

Edit : Instantiate a new scanner before calling nextInt, it should work :

try {
     Scanner console = new Scanner(System.in);
     int input = console.nextInt();
     switch (input) {
         ...

Solution 3:[3]

Since you use console.nextInt(), you will read only integers on the input. Then, when you enter something which is not an int, you catch an Exception. There you need to consume the bad input, e.g. with a nextLine():

        catch(Exception e) { // when entered a character
            System.err.println("Wrong Input"); 
            checkValue = false;
            console.nextLine(); // <=== add
        } 

Then it works...

Solution 4:[4]

You probably get java.util.InputMismatchException after inputting any illegal character. It does not clear the input buffer, so calling nextInt() causes the same error etc. Add console.next(); to the catch clause to skip the wrong character.

Solution 5:[5]

auswahl variable is not assigned with your input. Actually speaking this variable is not even in scene till you use it in switch.

boolean checkValue = false;
        int input;
        Scanner console = new Scanner(System.in);
            while (!checkValue) {

        System.out.println("Enter 1, 2 or 3");      

            try {
                    input = console.nextInt();

               switch (input) {
                    case 1:
                        // code 
                        checkValue = true;
                        break;
                    case 2:
                         // code 
                        checkValue = true;
                        break;
                    case 3:
                       // code 
                        checkValue =  true;
                        break;

                    default: // when entered a wrong number
                        System.err.println("Wrong Input"); 
                        checkValue = false;
                }
                }

                    catch(Exception e) { // when entered a character

                        System.err.println("Wrong Input"); 
                        checkValue = false;

                    }
            System.out.println("Program Ends");

Solution 6:[6]

This will work

catch (Exception e) { // when entered a character

            System.err.println("Wrong Input");
            checkValue = false;
            console = new Scanner(System.in);//Edit

}

Solution 7:[7]

boolean checkValue = true;

Scanner console = new Scanner(System.in);
while (checkValue) {

System.out.println("Enter 1, 2 or 3");      

    try {
            input = console.nextInt();

       switch (input) {
            case 1:
                // code 
                checkValue = false;
                break;
            case 2:
                 // code 
                checkValue = false;
                break;
            case 3:
               // code 
                checkValue =  false;
                break;

            default: // when entered a wrong number
                System.err.println("Wrong Input"); 
                checkValue = false;
        }
        }

            catch(Exception e) { // when entered a character

                System.err.println("Wrong Input"); 
                checkValue = false;

            } 
   }    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 T.Gounelle
Solution 4 pkalinow
Solution 5
Solution 6
Solution 7