-2

I'm making a scenario where I only want to accept 1 or 2 as input and also handling errors if another number is entered or if the input is not valid. For this I'm doing:

do {
        try {
            System.out.println("Enter 1 or 2");
            option = input.nextInt();
        } catch (InputMismatchException ex) {
            System.out.println("Invalid menu number entered. A valid menu option is required. Enter you choice again: ");
            //option = input.nextInt(); //The error occurs here
        }
    } while(!(option == 2 || option == 1));

If I add option = input.nextInt(); in catch it gives an error as the scope of option is within try. I want it to keep on asking user to input a valid number i.e. 1 or 2, but if I enter any character e it goes into InputMismatchException and exit.

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
  • What do you mean by "It's not working properly"? – TomekK Sep 02 '19 at 08:18
  • @TomekK updated the code see now. – Chaudhry Talha Sep 02 '19 at 08:34
  • In your code you have two scopes: First scope is the ``try-catch`` and the second scope is the ``while-loop``. The solution to your problem is to change the two scopes. Put the ``try-catch`` inside a ``while(true)`` loop and leave the ``while(true)`` when the user enters a ``0``. – Tobias Otto Sep 02 '19 at 08:40
  • @TobiasOtto doing so i.e. entering a char instead of int, it goes into a countless loop printing the statement in catch again and again. – Chaudhry Talha Sep 02 '19 at 08:47

1 Answers1

1

The simplest way to keep on asking is to use a do-while loop with while(true) as condition. Additionallyy you must call nextLine() method to clear possible invalid input out of its buffer. Otherwise, the bad input will trigger the same exception repeatedly in an infinite loop. Your code could look like something like:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int option;
    do{
        System.out.println("Enter 1 or 2");
        System.out.println();
        try{
            option = input.nextInt();
            switch(option){
                case 1: 
                    System.out.println("option: "+option);
                    break;
                case 2:
                    System.out.println("option: "+option);
                    break;
                default: 
                    throw new InputMismatchException();
            }
        }
        catch(InputMismatchException ex){
            input.nextLine();
            System.out.println("Invalid menu number entered. A valid menu option is required. Enter you choice again: ");
        }
    }while(true);
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28
  • I've changed it to `while(!(option == 2 || option == 1))` but if I enter any character lets say 'e' it go to exception and just quits meaning it didn't ask for input again. – Chaudhry Talha Sep 02 '19 at 08:33
  • You have no logic in your catch block which allows the program to ask the user for input again. – Eritrean Sep 02 '19 at 08:52
  • See the updated question. If I add `option = input.nextInt()` in catch it giver `Exception in thread "main" java.util.InputMismatchException` error – Chaudhry Talha Sep 02 '19 at 08:57
  • Have revised my answer according to your comments and changes. – Eritrean Sep 02 '19 at 10:00