调用Scanner.close()抛出异常 [英] Calling Scanner.close() throws nosuchelement exception

查看:260
本文介绍了调用Scanner.close()抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的方法,将命令打印到屏幕上,扫描用户的输入,并将其作为字符串返回。如果用户的输入无效,则通知用户并再次询问。这个方法是完美的,但是我的教练提到我们应该总是关闭资源,所以我回去并添加了close方法,现在我每次调用该方法时都会得到一个NoSuchElementException,无论用户输入什么。这是代码...

  private String getUserString(String userCommand){
System.out.println(userCommand );
扫描仪扫描=新扫描仪(System.in);
String userinput = scan.nextLine()。trim();

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

else {
return userinput;
}
}

异常总是回到userinput的行启动为scan.nextLine()。trim()
注意*我在每个返回语句之前的每一行添加了scan.close(),但是我没有在上面添加它。

解决方案

当您关闭扫描时,您正在关闭 System.in 当你尝试从它重新读取时,它会抛出异常。



您的教练是正确的一方面,您应该清理资源,只是当涉及到系统。在






而不是每次需要输入重新打开流,你可以创建您的扫描仪一次,只需在需要输入的地方重新使用:

  public static void main(String [] args)throws IOException {
扫描仪扫描=新的扫描仪(System.in);
System.out.println(getUserString(Write something:,scan));
System.out.println(getUserString(Write other 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;
}
}


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.

解决方案

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;
    }
}

这篇关于调用Scanner.close()抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆