Question: ```
import java.util.Scanner;
import java.util.InputMismatchException;
public class Demo
{
    public static void main(String [] agrs){
    Scanner keyBoard = new Scanner(System.in);
    int input;
    do{
        System.out.println("[ 1] Case 1.");
        System.out.println("[ 2] Case 2.");
        System.out.println("[ 3] Case 3.");
        System.out.println("[ 0] Case 0.");
        System.out.print("Your Choice: ");

        try{
            input = keyBoard.nextInt();
        }
        catch(InputMismatchException e){
            System.out.println("Error");
        }

        switch (input){
            default:
                  System.out.println("Default");
                  break;
            case 1:
                  System.out.println("One");
                  keyBoard.next();
                  break;
            case 2:
                  System.out.println("Two");
                  break;
            case 3:
                  System.out.println("Three");
                  break;
            case 0:
                  System.exit(0);
                  break;
        }
    }
    while(input != 0);
}

```

I want to make a menu in the console but taked a error.
Error: variable input might not have been initialized.
I know why i take the error but I don't know how to fix it.
I just know a little English so I expect mod edit my topic to fit more
Thank everybody

Answer: 
This should fix your problems with endless loops and invalid inputs, and your compiler error:

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

public class Demo {

    public static void main(String[] agrs) {

        Scanner keyBoard = new Scanner(System.in);
        // This fixes the compiler error!
        int input = -1;

        do {
            System.out.println("[ 1] Case 1.");
            System.out.println("[ 2] Case 2.");
            System.out.println("[ 3] Case 3.");
            System.out.println("[ 0] Case 0.");
            System.out.print("Your Choice: ");

            try {
                input = keyBoard.nextInt();
            } catch (InputMismatchException e) {
                // This fixes the endless loops on invalid inputs!
                System.out.println("Invalid input " + keyBoard.next());
                input = -1;
            }

            switch (input) {
                default:
                    System.out.println("Default");
                    break;
                case 1:
                    System.out.println("One");
                    keyBoard.next();
                    break;
                case 2:
                    System.out.println("Two");
                    break;
                case 3:
                    System.out.println("Three");
                    break;
                case 0:
                    System.exit(0);
                    break;
            }
        } while (input != 0);
    }
}

```
Its possible that an exception may be thrown from 'keyBoard.nextInt()' before input is actually assigned. If thats the case then think about what happens in the switch statement below... or even worse, how do you ever exit from the do-while loop.