0

Does anyone have idea why the console output show the menu one more time before printing the exception out?

I except the output should be:

1. item 1
2. item 2
3. Quit
Please choose a item:
WRONGINPUT         <---- user input
Invalid input      <---- where I want the exception shows
1. item 1
2. item 2
3. Quit
Please choose a item:

However, what I get is:

1. item 1
2. item 2
3. Quit
Please choose a item:
WRONGINPUT         <---- user input
1. item 1
2. item 2
3. Quit
Please choose a item:
Invalid input      <---- why the exception is shown here?

The code is shown below:

    // code omitted

    Scanner scanner = new Scanner(System.in);
    int mainMenu = -1;
    do {    
        try {
            System.out.println("1. item 1");
            System.out.println("2. item 2");
            System.out.println("3. Quit");
            System.out.println("Please choose a item:");
            mainMenu = scanner.nextInt();
        } catch (InputMismatchException e) {
            scanner.nextLine(); 
            System.err.println("Invalid input");        
        }
            if (mainMenu == 1)
                // do something
            else if (mainMenu == 2)
                // do something
            else if (mainMenu == 3)
                System.out.println("Quitting...");
    } while (mainMenu != 3);

console output

mmcc88
  • 177
  • 1
  • 10
  • Can you add the initialization of `scanner` please? – TDG May 20 '16 at 01:38
  • the initialization of `scanner` is above the `do-while` loop. but still got this incorrect flow – mmcc88 May 20 '16 at 01:45
  • @TDG yea I ran it from Eclipse. I attached the screen of output above. – mmcc88 May 20 '16 at 01:54
  • I don't know why Eclipse behaves like that. Run it from the command line and everything will be fine. – TDG May 20 '16 at 01:54
  • Take a look here http://stackoverflow.com/questions/8684303/c-program-output-in-wrong-order-eclipse – TDG May 20 '16 at 02:01
  • Ran your code on my Eclipse (Luna), and it behaves fine. Can you try the 'ol Restart Eclipse? – jmcg May 20 '16 at 02:04
  • @TDG That's really weird... even I used the Debug function to step through every steps, it jumped to the catch block right after the wrong input and then back to menu part. But when I Run it, the console output is different from what Debug showed me. – mmcc88 May 20 '16 at 02:05

2 Answers2

0

Here is answer.Please run this program.

package java7.demo;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class Test {

    public static void main(String args[]){
        int mainMenu = -1;
         Scanner scanner = new Scanner(System.in);

    do {    
        try {
            System.out.println("1. item 1");
            System.out.println("2. item 2");
            System.out.println("3. Quit");
            System.out.println("Please choose a item:");
            mainMenu = scanner.nextInt();
            if (mainMenu == 1){
                // do something
            }
            else if (mainMenu == 2){

            }
                // do something
            else if (mainMenu == 3){
                System.out.println("Quitting...");
            }else{
                throw new InputMismatchException();
            }
        } catch (InputMismatchException e) {

            System.err.println("Invalid input"); 
            scanner.nextLine(); 
        }

    } while (mainMenu != 3);
    }
}

You only need to change scanner.nextLine() under err print statement.

sawyinwaimon
  • 739
  • 1
  • 6
  • 14
0

My code has nothing wrong in the order when I run it in command.

I find that the reason is System.err.println matters in Eclipse. When I change it to System.out.println, I get correct output order. But I think it is not necessary as this is the problem with Eclipse.

Console Printing Order in Eclipse

This link gives me the reason. Anyways thanks for your help. Cheers

Community
  • 1
  • 1
mmcc88
  • 177
  • 1
  • 10