4

I am trying to get text input from the keyboard in Java 6. I am new to the language and whenever i run the following code, I get this error:

package test1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
    boolean quit = false;
    while (!quit){
        Scanner keyIn;
        String c = "x";
        while (c != "y" && c != "n") {
            keyIn = new Scanner(System.in);
            c = keyIn.next();
            keyIn.close();
        }
        if (c == "n")
            quit = true;
    }
 }
}


Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at test1.Test.main(Test.java:11)

Am I mis-using the next() method? I thought it would wait for user input but it looks like it isn't and throwing the exception saying that there is nothing left in the scanner.

2
  • 1
    what is your code doing exactly and what's the input?
    – Victor
    Jul 2, 2009 at 0:30
  • all this is is trying to get the user to input "y" or "n" and if its "y" keep looping, otherwise exit. the error happens no matter which letter i press. Jul 2, 2009 at 0:31

4 Answers 4

8

The reason for the exception is that you are calling keyIn.close() after you use the scanner once, which not only closes the Scanner but also System.in. The very next iteration you create a new Scanner which promptly blows up because System.in is now closed. To fix that, what you should do is only create a scanner once before you enter the while loop, and skip the close() call entirely since you don't want to close System.in.

After fixing that the program still won't work because of the == and != string comparisons you do. When comparing strings in Java you must use equals() to compare the string contents. When you use == and != you are comparing the object references, so these comparisons will always return false in your code. Always use equals() to compare strings.

while (!quit){
    Scanner keyIn = new Scanner(System.in);
    String c = "x";
    while (!c.equals("y") && !c.equals("n")) {
        c = keyIn.next();
    }
    if (c.equals("n"))
        quit = true;
}
1
  • Thank you very much, this answer solved mine problem too! I didn't know that 'in.close();' also closes 'system.in' !
    – Redauser
    Dec 26, 2014 at 11:44
2

To evaluate strings you have to use .equals

while(!c.equals("y")) { do stuff...

1
  • declare your Scanner reference outside your loops. you don't have to create it and close it every time.

  • compare string text with the method equals, not with the operator ==.

0

Try using nextLine() and only looking at the first element in the string that is returned.

The != and == will only work when used against characters or other primitive types, that will only work in c#. You will need to use .equals to ensure you are checking for proper equality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.