简体   繁体   中英

java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1585)

I'm prompting for user input over and over again until the user types exit in which case the data object will be set to null and it will leave the loop. For the most part this code works, but sometimes I get a NoSuchElementException on the line: reader = new Scanner(System.in); . This tends to happen after a few loops and not the first time through. If I alternatively declare the reader variable within the scope of the while loop then it will fail on the second loop.

Mostly Working (Sometimes throws exception)

int level = 1;  
Scanner reader;
String selection = null;

while (true) {
    if (data.done) {
        level--;
    }

    data.done = false;
    System.out.println(getSubmenu(level, data));

    reader = new Scanner(System.in);

    if (level <6) {
        selection = reader.nextLine();
    } else {
        level = 4;
    }

    if (validSelection(selection)) {
        level = getLevel(level, selection);
        data = getData(level, data, selection);
    } else {
        System.out.println("Invalid entry");
    }

    if (data == null) {
        System.out.println("Level "+ level + "selection " + selection);
        break; // exit command was typed
    }
}
reader.close();

Alternate (throws exception on second loop)

int level = 1;  
String selection = null;

while (true) {
    if (data.done) {
        level--;
    }

    data.done = false;
    System.out.println(getSubmenu(level, data));

    Scanner reader = new Scanner(System.in);

    if (level <6) {
        selection = reader.nextLine();
    } else {
        level = 4;
    }

    if (validSelection(selection)) {
        level = getLevel(level, selection);
        data = getData(level, data, selection);
    } else {
        System.out.println("Invalid entry");
    }

    if (data == null) {
        System.out.println("Level "+ level + "selection " + selection);
        break; // exit command was typed
    }
    reader.close();
}

StackTrace

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at org.functional.utils.Menu.run(ServerScripts.java:61)
at org.functional.utils.ServerScripts.main(ServerScripts.java:18)

What am I missing?

Allocate the Scanner at the start of your code, not each time through the loop:

int level = 1;  
Scanner reader = new Scanner(System.in);
String selection = null;

while (true) {
    if (data.done) {
        level--;
    }

    data.done = false;
    System.out.println(getSubmenu(level, data));

    if (level <6) {
        selection = reader.nextLine();
    } else {
        level = 4;
    }

    if (validSelection(selection)) {
        level = getLevel(level, selection);
        data = getData(level, data, selection);
    } else {
        System.out.println("Invalid entry");
    }

    if (data == null) {
        System.out.println("Level "+ level + "selection " + selection);
        break; // exit command was typed
    }
}
reader.close();

The way you were doing it, each Scanner that you create is left orphaned at the next loop iteration. None of them were being closed. The exception most likely arises when some internal resource is consumed from all those garbage Scanner objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM