3

I was having some problem when trying to do a try catch for do while loop:

try{
    do {
        System.out.println("Enter your option: ");
        choice = sc.nextInt();
        switch (choice) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;

        }
    } while (choice != 6);
    }catch(InputMismatchException e){
        System.out.println("Please enter option between 1-6.");
    }

What I am trying to do for the do while loop is when user entered anything other than 6 which is terminate, it will keep prompting for user input. For each case, it will go to certain method.

Then, I tried to do a try catch for InputMismatchException because my Scanner is taking integer from user input. However, after I entered alphabet instead of integer, the program just terminated itself. I am trying to do like when user entered alphabet, it will keep on prompting user for correct input.

Any ideas? Thanks in advance.

I was thinking if I should make another do while to wrap the entire try catch?

3
  • 1
    If an exception is thrown, then it will take you to your catch which is outside your while loop and hence terminate. Simply put your try-catch more local to where you wish to catch exceptions i.e. inside the for loop. Commented Mar 21, 2016 at 15:48
  • Yeah I shifted it in as suggested from one of the answer. But the problem is when user entered alphabet, the program gives me an infinite loop of "Enter your option: " without taking user input Commented Mar 21, 2016 at 15:49
  • So long as you redefine the variable that you're checking for in your input (the one inside the try-catch), then it won't be an "infinite" loop. It will be exitable by the user entering valid input and then progressing. Another way would be to not use nextInt at all and simply get the string input from the user and validate it yourself, rather than using try-catch mechanisms at all. Commented Mar 21, 2016 at 15:52

2 Answers 2

4

Do like :

try {
   choice = sc.nextInt();
} catch(InputMismatchException e){
        System.out.println("Please enter option between 1-6.");
        sc.next();
        continue;
    }

If user enters a invalid input it will go to the catch block and will continue the loop. Remove the outer try catch block. Its not required

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm this actually giving me infinite looping which when user entered alphabet, the option menu itself just keep looping without taking user input. I removed the outer try catch did it as the way you suggested but unfortunately no luck.
Your break statements apply to the switch (not the loop), and you need to call Scanner.next() when you don't get an int (or the token will cause an infinite loop).
Sorry but would you mind to show me some code example? I am confused
I see I see. That solved the problem. Thank you so much!
0

To handle characters and and invalid numbers you could do something like this:

do {
    System.out.println("Enter your option: ");
    try{
      choice = sc.nextInt();
    catch(InputMismatchException e){
      choice = 0;
      sc.next();
    }
    switch (choice) {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        default:
            System.out.println("Please enter option between 1-6.");
            break;
       }
} while (choice != 6);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.