Scanner returning error

Asked

Viewed 37 times

1

I’m writing the code below:

    public int findNet(String InputRede) {
    try {
        for (GetNetwork network : getRedes()) {
            if (network.getId().equals(InputRede)) {
                return getRedes().indexOf(network);
            }
        }
    } catch (NullPointerException exc) {
        System.out.println("Erro! a interface não é wireless! por favor escolher outra!");
    }
    return -1;
}

public void SelRede() {
    Scanner scanner2 = new Scanner(System.in);
    System.out.println("Digite a ID da rede");
    idNum = findNet(scanner2.nextLine());
    while (idNum == -1) {
        System.out.println("ID de rede não encontrada! digite novamente!");
        idNum = findNet(scanner2.nextLine());
    }
    System.out.println("Digite a senha da rede (deixe em branco caso não haja)");
    senha = scanner2.nextLine();
    scanner2.close();
}

However, he has returned me the following error:

Exception in thread "main" java.util.Nosuchelementexception: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at classes.control.Selectnetwork.Selrede(Selectnetwork.java:28)

The line 28 is this:

idNum = findNet(scanner2.nextLine());

Briefly, what is happening is that the code is not running the scanner, not allowing the user to insert the text into the variable, returning the Exception Nosuchelementexception.

My question is not as follows::

Because the code is not running the scanner?

  • By variable name, you must be using another scanner and closed it, with this ai still open. The reason for this exception is the same as the duplicate.

  • Thank you! I will check, but I believe that’s right. This class is linked to another one that also works with scanner.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.