简体   繁体   中英

Java Scanner strange behavior

I have a java scanner and two loops to handle user input, However it throws an NoSuchElement exception the second it hits the first loop with out asking for any input from the user.

Scanner Guess_input = new Scanner( System.in );


    while (guess > 0){
        failure = true;
        while(failure)
        {

            System.out.println("Please input");
            try
            {
                if (Guess_input.nextLine().length() == 1 && guesses.size() >= 1) {
                    guesses.add(Guess_input.nextLine());
                    System.out.println("You guessed" + guesses.get(guesses.size()) + "");
                }
                else if (Guess_input.nextLine().length() == 0) {
                    System.err.println("ERROR:");
                    Guess_input.nextLine();   //Clean Buffer
                    failure = true;
                }
                else 
                {
                    System.err.println("ERROR");
                    Guess_input.nextLine();   //Clean Buffer
                    failure = true;
                }
            }   
            catch(InputMismatchException ime)

            {
                System.err.println("error");
            }
            finally
            {
                Guess_input.close();
            }
        }
    }

From the java documentation, when using the next() method of the Scanner class, you'll get

NoSuchElementException - if no such tokens are available

Whenever you call the nextLine() method, you are supposed to enter a String . You should first store the result of nextLine() in local variable unless that's what you want.

Another problem is that your try catch finally is done in your while loop . It means that for each iteration, your finally bloc will be executed everytime, so you'll think that there is an exception, while might be none. Apply these changes

try {
   while (guess > 0) {
       while (.....) {
           .....
       }
   }
} catch (...){
   ....
  }
finally{ .... }

The errant statement is guesses.get(guesses.size()) . In Java lists use zero-based indexes, ie the index of the first element is always 0 and the last element is size - 1. By definition the size of a list is an invalid index.

You probably should just hold the next line in its own variable before adding it to the list so that your sysout statement can just reference the variable instead of pulling the value back out of the list. But the easy solution is to just change the code to guesses.get(guesses.size() - 1)

You're calling guesses.nextLine() way too many times. Every call to nextLine() will block the app and expect input. Furthermore, theres other issues to worry about there... like other people pointed out.

I'll stick to the scanner though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM