0

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.

Shiri
  • 1,972
  • 7
  • 24
  • 46

3 Answers3

3

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.

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
0

You have to close the scanner when everything is done.

Amios
  • 61
  • 8
0

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.

Rachit Ahuja
  • 371
  • 2
  • 15
  • Even without the twice closing, the error still persists. Also, if the closing occurs well after the '.nextLine()' how does the stream close before picking up the line? – Shiri Jul 22 '15 at 11:42