Java Scanner no line found, and then Scanner closed error?

偶尔善良 提交于 2019-12-31 07:24:07

问题


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.

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.

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.


回答1:


Scanner.close() documentation states that

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.

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.

Refer : java.util.NoSuchElementException - Scanner reading user input

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


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.




回答2:


You have to close the scanner when everything is done.




回答3:


You have closed the scanner inout stream readIn.close(); twice.

You are closing the stream before picking line by line from the file. So you have to close it once after all the instances that use readIn is finished.



来源:https://stackoverflow.com/questions/31561376/java-scanner-no-line-found-and-then-scanner-closed-error

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