Using try(Scanner scan = new Scanner(System.in)) { }
is causing
Exception in thread \"main\" java.util.NoSuchElementException
You're closing System.in
(a global-variable). Please, do not do that. Everywhere you have
try(Scanner scan = new Scanner(System.in))
guarantees that System.in
will be close
(d). Once it's close
(d) you can't read from it again (or you get your mentioned Exception
). Also, you can compile with debug symbols (or step into it with your IDE's built-in debugger or jdb as applicable). The Scanner.close() Javadoc says (in part),
If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's
close
method will be invoked
Have you tried not using the try?
String username;
String password;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Username: ");