1

I'm a new programmer. I'm coding a user menu and I've a question on the do-while loop. When main() calls my first method containing my first loop it works as expected. However, when the user selects makes a choice and customerMenu() is called, it prints the menu 3 times. Why is this? Is there a mistake in my code?


public class Runner {

    public static void main(String[] args) {
        Menu m = new Menu ();

        m.mainMenu();
    }
}

public class Menu {

    private char choice;

    public void mainMenu () {

        try {
            do {
                System.out.println("Create Order");
                System.out.println("View Orders");
                System.out.println("Customers");
                System.out.println("Employees");

                choice = (char) System.in.read();

            } while (choice < '1' || choice > '4');

            System.out.println("\n");

            switch (choice) {
            case '1':
                System.out.println("Create Order page");
                break;
            case '2':
                System.out.println("View Orders page");
                break;
            case '3':
                customerMenu();
                //System.out.println("Customers page");
                break;
            case '4':
                System.out.println("Employees page");
                break;
            }

        } catch (Exception e) {
            System.out.println("Error");
        }
    }

    // Do loop prints 3 times

    public void customerMenu () {

        try {

            do {
                System.out.println("Add a Customer");
                System.out.println("Edit a Customer");
                System.out.println("Delete a Customer");

                choice = (char) System.in.read();

            } while (choice < '1' || choice > '3');


            System.out.println("\n");

            switch (choice) {
            case '1':
                System.out.println("Add a Customer action");
                break;
            case '2':
                System.out.println("Edit a Customer action");
                break;
            case '3':
                System.out.println("Delete a Customer action");
                break;
            }

        } catch (Exception e) {
            System.out.println("Error");
        }
    } 
}
Turing85
  • 13,364
  • 5
  • 27
  • 49
Frank
  • 33
  • 2

3 Answers3

1

There are no problems with the logic of your program but instead of using System.in.read () use either Scanner or BufferedReader. I can only guess that the three other characters that System.in.read () read are the null byte, line feed, and carriage return.

alvinalvord
  • 384
  • 2
  • 11
0

I just tried out the code (link to test env) and for me the menu is printing two times, not three.

The reason it prints twice is because System.in.read() reads in the next byte of data. When you type '3' and hit enter, that sends two bytes of data to the input stream. One byte for the 3 and one byte to indicate 'newline'.

The first time the do-while loop is entered, it reads in the 'newline' byte. That causes the loop to run a second time. The second time around, there's no more bytes to read so it waits for input for user.

Kevin Aud
  • 360
  • 2
  • 12
0

Hi there i found your're mistake, i am not good with java. hmm maybe just printing the choice variable you will find your answer.

in mainmenu you have the choice variable and when you called customerMenu on the do while loop the choice variable is read again.

solution create another variable for customerMenu.

Azures
  • 21
  • 5