user12476460
user12476460

Reputation:

Can't get Java do/while to process correctly

I've reviewed several other questions similar to this, but the ones I read didn't help solve my issue. My goal is to print the menu value based on the int user input, and continue offering a selection until the user hits 6 to exit out. Here is my code (I'm new to Java).

class BookCategories {

    public static void main(String args[]) throws java.io.IOException {

        String menu = "MENU: \n1. Science Fiction \n2. Computers and Technology \n3. Cooking \n4. Business \n5. Comics \n6. Exit";

        Map<Integer, String> bookMap = new HashMap<Integer, String>();
        bookMap.put(1, "You selected Science Fiction. This is a fun topic.");
        bookMap.put(2, "You selected Computers and Technology. This is a complex topic.");
        bookMap.put(3, "You selected Cooking. This is a passionate topic.");
        bookMap.put(4, "You selected Business. This is a serious topic.");
        bookMap.put(5, "You selected Comics. This is an entertaining topic.");
        // bookMap.put(6, " ");

        int x;

        do {
            System.out.println(menu);
            System.out.println("Select an item from the Menu, then press Enter: ");

            x = (int) System.in.read();

            if (x == 6)
                System.out.println("You exited the menu.");
            else {
                System.out.println(bookMap.get(x));
                if (x < 1 || x > 6)
                    System.out.println("Invalid entry. Please enter a number from 1 to 6.");
                else
                    System.out.print(menu);
                System.out.println("Select an item from the Menu, then press Enter.");
            }

        } while (x != 6);
    }

}

Upvotes: 0

Views: 61

Answers (2)

Daniel
Daniel

Reputation: 1460

As @user15793316 already said, System.in.read() does not read the actual number. You can make use of Scanner instead:

Scanner scanner = new Scanner(System.in);

do {
  // ...
  x = scanner.nextInt();
  scanner.nextLine(); // wait for Enter
  // ...
}

Upvotes: 1

SM2A
SM2A

Reputation: 535

Well, for some reason System.in.read(); was not working properly so I change your code this way :

public static void main(String[] args) throws java.io.IOException{

    String menu = "MENU: \n1. Science Fiction \n2. Computers and Technology \n3. Cooking \n4. Business \n5. Comics \n6. Exit";
    Scanner scanner = new Scanner(System.in); // this line

    Map<Integer, String> bookMap = new HashMap<Integer, String>();
    bookMap.put(1, "You selected Science Fiction. This is a fun topic.");
    bookMap.put(2, "You selected Computers and Technology. This is a complex topic.");
    bookMap.put(3, "You selected Cooking. This is a passionate topic.");
    bookMap.put(4, "You selected Business. This is a serious topic.");
    bookMap.put(5, "You selected Comics. This is an entertaining topic.");
    // bookMap.put(6, " ");

    int x;

    do {
        System.out.println(menu);
        System.out.println("Select an item from the Menu, then press Enter: ");

        x = scanner.nextInt(); // and this line

        if (x == 6)
            System.out.println("You exited the menu.");
        else {
            System.out.println(bookMap.get(x));
            if (x < 1 || x > 6)
                System.out.println("Invalid entry. Please enter a number from 1 to 6.");
            else
                System.out.print(menu);
            System.out.println("Select an item from the Menu, then press Enter.");
        }

    } while (x != 6);

}

Upvotes: 0

Related Questions