Hi all, I've got a problem relating to error reporting. In the code posted below, when the user actually presses enter after reading the error, the program does not loop back to the main menu, but merely repeats the error. If I remove the "input.nextLine();" code in line 17, then the program does loop back to the main menu. Anyone got any idea as to what the problem could be?

do {

            try {

                System.out.println("\nWelcome to Telephone Directory!");
                System.out.println("===============================");
                System.out.println("\n1.)Add Record");
                System.out.println("2.)Search Records");
                System.out.println("3.)Edit Record");
                System.out.println("4.)Delete Record");
                System.out.println("5.)Display All Records");
                System.out.println("6.)Quit");
                menuChoice = input.nextInt();
            } catch (Exception e) {
                Scanner input = new Scanner(System.in);
                System.out.println("\nERROR: CHOICE MUST BE OF INTEGER NATURE. \nPress ENTER to return to main menu");
                input.nextLine();
            }

        } while (menuChoice != 6);

Thanks :)

Recommended Answers

All 10 Replies

The Scanner class buffers the input. If the call to next... sees the wrong data it will fail with an Exception, but the wrong data is still there. You need to nextLine call to clear the bad data.
Why do you create a new instance of the Scanner class in the catch() block vs using the same one being used before.

Could you copy and post the console to show what you are seeing. I'm not sure of your description.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

The Scanner class buffers the input. If the call to next... sees the wrong data it will fail with an Exception, but the wrong data is still there. You need to nextLine call to clear the bad data.
Why do you create a new instance of the Scanner class in the catch() block vs using the same one being used before.

Could you copy and post the console to show what you are seeing. I'm not sure of your description.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Hi, I created a new instance of the scanner class because for some reason the compiler ignores the "input.nextLine();" command without it. It does not let me input anything and merely displays the error message and then loops back to the main menu

Why do you think it ignores that statement?
Change your code to show what was read by the nextLine():
System.out.println("nextLine=" + input.nextLine() + "<"); // show what is being ignored

Why do you think it ignores that statement?
Change your code to show what was read by the nextLine():
System.out.println("nextLine=" + input.nextLine() + "<"); // show what is being ignored

Tried it, but nothing showed up :S

Could you copy and post the console to show what you are seeing. I'm not sure of your description.

Could you copy and post the console to show what you are seeing. I'm not sure of your description.

This is what I'm seeing:

Welcome to Telephone Directory!
===============================

1.)Add Record
2.)Search Records
3.)Edit Record
4.)Delete Record
5.)Display All Records
6.)Quit
s

ERROR: CHOICE MUST BE OF INTEGER NATURE.
Press ENTER to return to main menu.

Welcome to Telephone Directory!
===============================

1.)Add Record
2.)Search Records
3.)Edit Record
4.)Delete Record
5.)Display All Records
6.)Quit

After it says "Please press ENTER" to return to main menu, it just returns to the main menu without allowing me to press enter.

It's funny that you don't see the output from the println():
System.out.println("nextLine=" + input.nextLine() + "<");

I would expect it to be executed when the error message is shown.

Did you leave it out? Try it again with that println() in the code.

It's funny that you don't see the output from the println():
System.out.println("nextLine=" + input.nextLine() + "<");

I would expect it to be executed when the error message is shown.

Did you leave it out? Try it again with that println() in the code.

ERROR: CHOICE MUST BE OF INTEGER NATURE.
s
nextLine=s<

Welcome to Telephone Directory!
===============================

1.)Add Record
2.)Search Records
3.)Edit Record
4.)Delete Record
5.)Display All Records
6.)Quit

... seems to work like that. If i take off the code which you told me to add, it just loops back to the main menu as you saw in code i pasted before

The Scanner class buffers the input. If the call to next... sees the wrong data it will fail with an Exception, but the wrong data is still there. You need to call nextLine call to clear the bad data.

The above output you posted shows you what is happening. The 's' is still in the buffer. When you call nextLine() it reads the 's' and the program continues.

The above output you posted shows you what is happening. The 's' is still in the buffer. When you call nextLine() it reads the 's' and the program continues.

SO what exactly do I have to do for the scanner to read nothing, i.e. the enter key?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.