Calling Scanner.close() throws nosuchelement exception

ぐ巨炮叔叔 提交于 2020-01-21 12:11:05

问题


I have a simple method that prints a command to the screen, scan's a user's input, and returns it as a String. If the user's input is invalid, it notifies the user and asks again. This method worked perfectly but my instructor mentioned that we should always close resources, so I went back and added in the close method and now I'm getting a NoSuchElementException every time the method is called, regardless of user input. Here is the code...

private String getUserString(String userCommand) {
    System.out.println(userCommand);
    Scanner scan = new Scanner(System.in);
    String userinput = scan.nextLine().trim();

    if (userinput.isEmpty()){
        System.out.println("Invalid choice");
        return getUserString(userCommand);
    }

    else {
        return userinput;
    }
}

The exceptions always point back to the line where userinput is initiated as scan.nextLine().trim() NOTE* I added scan.close() on each line before each return statement, however I didn't include it above.


回答1:


When you close scan you are closing System.in and when you try to re-read from it, it will throw the exception.

Your instructor is right in one aspect, you should clean up resources, just not when it comes to System.in.


Instead of re-opening the stream every time you need input you could create your Scanner once and just re-using it where you need input:

public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(System.in);
    System.out.println(getUserString("Write something:", scan));
    System.out.println(getUserString("Write something else:", scan));
}

private static String getUserString(String userCommand, Scanner scan) {

    System.out.println(userCommand);

    String userinput = scan.nextLine().trim();

    if (userinput.isEmpty()) {
        System.out.println("Invalid choice");
        return getUserString(userCommand, scan);
    } else {
        return userinput;
    }
}


来源:https://stackoverflow.com/questions/12454662/calling-scanner-close-throws-nosuchelement-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!