[Solved] Scanner – Exception in thread “main”


From the documentation:

public class NoSuchElementException extends RuntimeException Thrown
by the nextElement method of an Enumeration to indicate that there are
no more elements in the enumeration.

However, when I tried your code on Eclipse, it’s running free of errors.
But when I tried it on Code Playground, it gave me the same error.

You have entered the following username: 
You have entered the following password: 
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at LoginSystem.runConsole(LoginSystem.java:25)
    at LoginSystem.main(LoginSystem.java:11)

It seems it’s a bug in their system. Because you need to input the userName and password for three loops in advance in one go at the very beginning of the run.
I went around this by wrapping the scan with hasNextLinewhich Returns true if there is another line in the input of this scanner.

So, it becomes:

if(console.hasNextLine()){
   userName = console.nextLine();
}
if(console.hasNextLine()){
   password = console.nextLine();
}

solved Scanner – Exception in thread “main”