#It throws nosuchelementexception

8 messages · Page 1 of 1 (latest)

scenic magnet
#

This is my code
The error falls around this part of the code:
public void Crearusuario() {
String user = "";
String password = "";
Scanner scanner = new Scanner(System.in);
System.out.print("Escribe un nombre de usuario: ");
user = scanner.nextLine();
System.out.print("Escribe una clave: ");
password = scanner.nextLine();

    String hashedpassword = Integer.toString(password.hashCode());
    userdata.put(user, hashedpassword);
    datab.put(user, datos);
    // scanner.close();
}

The output is
Escribe un nombre de usuario: Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Terminal.Crearusuario(Terminal.java:40)
at Terminal.main(Terminal.java:133)

bronze surgeBOT
#

This post has been reserved for your question.

Hey @scenic magnet! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

surreal mason
#

Are you requesting user input from multiple methods?

#

Are you calling the method multiple times?

#

Once you call close(), you cannot use System.in again unless you restart the program

sick parcel
#

Create only one Scanner object and create it outside of any methods...

#

And you should be able to avoid this error.