awyeanah2
awyeanah2

Reputation: 148

Scanner needs inputs twice before continuing

Hey guys i have got a small weird problem here, i am asking the user to input their menu choice and depending on what they choose it calls a certain method.

I have used scanner.next() after some googling but for some reason only when i enter 1 or 2, i press enter and then press say 1 again and then it actually works. But what is weird that it calls options 3, 4, 5 and 6, immediately without me having to input the number twice.

I have tried with scanner.nextLine() after the scanner.nextInt() and that just leaves me having to put my option 1 or 2 in with no result.

while(exit == 0)
    {

            System.out.println("\n");
            System.out.println("Menu 1: Display fullname of the user \n");
            System.out.println("Menu 2: Display of user information \n");
            System.out.println("Menu 3: Change password \n");
            System.out.println("Menu 4: List all of users in the library full name\n");
            System.out.println("Menu 5: Search for a book\n");
            System.out.println("Press 6 to search for a books location in the library\n");
            System.out.println("Press 0 to exit\n");

            System.out.println("Enter choice: ");
            int menuChoice = scanner.nextInt();
            scanner.next();


                     if(menuChoice == 1)
                    {
                        displayUserFullName();
                    }
                    else if(menuChoice == 2)
                    {
                        displayUserInformation();
                    }

                    else if(menuChoice == 3)
                    {
                        menuForChangePassword();
                    }

                    else if(menuChoice == 4)
                    {
                        displayAllUserInSystem();
                    }
                    else if(menuChoice == 5)
                    {
                        searchBookByISBN();
                    }
                    else if(menuChoice == 6)
                    {
                        searchBookLocation();
                    }
                    else if(menuChoice == 0)
                    {
                        exit = 1;
                    }
}

Thank you in advance!

Upvotes: 0

Views: 1302

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109547

Scanner is a class parsing single tokens, like nextInt, nextDouble, nextToken (String). With corresponding testing methods: hasNextInt and so on. All this parsing you do not need, so use nextLine for an entered line, or an other Reader class (InputStreamReader, BufferedReader).

Also you may utilize switch instead of if else if.

       String menuChoice = scanner.nextLine();
       switch (menuChoice) {
       case "1":
           displayUserFullName();
           break;
       case "2":
           displayUserInformation();
           break;
       case "3":
           menuForChangePassword();
           break;
       case "4":
           displayAllUserInSystem();
           break;
       case "5":
           searchBookByISBN();
            break;
       case "6":
           searchBookLocation();
           break;
       case "0":
           exit = 1;
           break;
       default:
           System.out.printf("Unknown choice: '%s'%n", menuChoice);
       }

menuChoice will contain the entire line, without line ending.

You might use an int with Integer.parseInt(menuChoice) but this would throw a NumberFormatException on wrong input, aborting your program. Scanner.nextInt would hang too, actually needing an hasNextInt().

Upvotes: 0

jhamon
jhamon

Reputation: 3691

int menuChoice = scanner.nextInt();
scanner.next();

Read the javadoc for scanner. It waits for user input:

public String next(): [..] This method may block while waiting for input to scan

So in your program, you say: wait for user to type and int, then wait for user to type something.

Remove the scanner.next(); and it should work.

Upvotes: 4

Related Questions