Java Scanner找不到行,然后Scanner关闭错误? [英] Java Scanner no line found, and then Scanner closed error?

查看:183
本文介绍了Java Scanner找不到行,然后Scanner关闭错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java代码要求用户输入,然后将这些数据存储在字符串变量中。下面的函数是一个名为'number'的类的一部分,在main函数中调用。

I have Java code that asks for user input and then stores this data in a string variable. The below function is part of a class called 'number' and is called in the main function.

        public static void setVal(int i){

    Scanner readIn = new Scanner(System.in);
    //while (readIn.hasNextLine()){
        str = readIn.nextLine();

        numCheck = false;

        if (i == 1){
            while (!numCheck){

                if (str.contains(" ")){
                    System.out.println("Please input a single item.");
                    str = readIn.nextLine();
                }
                else if (!isNumeric(str)){
                    System.out.println("Please input a valid number.");
                    str = readIn.nextLine();
                }
                else {
                    numCheck = true;
                    value = Double.parseDouble(str);
                    readIn.close();
                }
            }
            readIn.close();
        }

        else if (i == 2){
            while (!numCheck){

                if (str.contains(" ")){
                    System.out.println("Please input a single item.");
                    str = readIn.nextLine();
                }
                else if (!isNumeric(str)){
                    System.out.println("Please input a valid number.");
                    str = readIn.nextLine();
                }
                else {
                    numCheck = true;
                    secondV = Double.parseDouble(str);
                    readIn.close();
                }
            }
            readIn.close();
        }

        else {
            System.out.println("An error has occurred.");
        }
//  }
    readIn.close();
}

主要功能的一部分如下所示:

Part of the main function looks like this:

     number input = new number();

    for (int i = 1; i <= 2; i++){

        input.setVal(i);

        System.out.println("Now please input a second value for computing with the first.");

        input.setVal(i);

    }

我使用相同的函数两次但是给它一个不同的参数将输入的分配区分为不同的变量,但是当它第二次运行时,它会抛出一个无行找到错误。

I use the same function twice but handing it a different argument to distinguish assignment of the input to a different variable but when it runs a second time it throws a no line found error.

应用其他一些建议你可以看到注释掉了我在执行代码之前添加了'hasNextLine()'检查以检查该行是否存在,但这最终导致'Scanner closed'错误,即使我每次运行该函数时都调用一个新的Scanner实例。我还适当地关闭了扫描仪以确保最小化错误。

Applying some other advice you can see commented out I have added a 'hasNextLine()' check to check if the line exists before executing the code but this ends up at a 'Scanner closed' error even though I invoke a new instance of Scanner every time the function runs. I have also closed the scanner appropriately to ensure minimisation of errors.

我不知道出了什么问题因为我可以在main函数中创建一个Scanner并调用'.nextLine ()'在没有错误的情况下需要多次,但是当通过类方法再次调用时,我收到这些错误。

I have no idea what's going wrong as I can create a Scanner in the main function and call '.nextLine()' as many times as requried without an error but when called again through a class method, I receive these errors.

感谢任何帮助,提前感谢。

Any help is appreciated, thanks in advance.

推荐答案

Scanner.close()文档说明


如果此扫描程序尚未关闭,那么如果它的底层
可执行也实现了Closeable接口,那么将调用可读的
close方法。如果此扫描仪已关闭,那么调用此方法的
将无效。

If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.

关闭扫描仪时,您也将关闭System.in输入流,所以当你重新打开扫描仪时,它将找不到任何打开的输入流。

On closing scanner, you are also closing System.in input stream, so when you reopen the scanner it will not find any open input stream.

参考:java.util.NoSuchElementException - 扫描仪阅读用户输入

更好将外部方法中的scanner对象作为参数传递,然后只有在完成后才能在调用方法中将其关闭。

Better pass scanner object from outside method as argument and then close it in calling method only when you are done with it.

要指出的是,你的String对象 str 静态?
如果没有,则不能在静态方法中使用它。最好从方法声明中删除静态。

Just to point out, is your String object str Static? If not then you can't use it in your static method. Better you remove the static from method declaration.

这篇关于Java Scanner找不到行,然后Scanner关闭错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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