How do I make java wait for input before assigning the input to a variable to avoid java.util.NoSuchElementException

Viewed 149

I'm studying Java classes and I'm trying to create a code where the user inputs how many objects (in this case "cube") they want to create.

In my main class I have this code written

System.out.println("Enter the amount of objects you want to create");
Scanner objNumInput = new Scanner(System.in);
int objNum = objNumInput.nextInt();
objNumInput.close();
Cube cubes[] = new Cube[objNum];

for (int i = 0; i < objNum; i++){
  String cubeName = Cube.inputName();
  double cubeLength = Cube.inputLength();
  cubes[i] = new Cube(cubeName, cubeLength);
}

in my Cube class I have here:

public static String inputName(){
  String cubeName;
  Scanner input = new Scanner(System.in);
  System.out.println("Enter the name: ");
  cubeName = input.nextLine();
  return cubeName;
}
public static double inputLength(){
  double cubeLength;
  Scanner input = new Scanner(System.in);
  System.out.println("Enter the length: ");
  cubeLength = input.nextDouble();
  return cubeLength;
}

When I run it, I can input the number of "cubes" I want to create. Then, it keeps throwing an exception

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Cube.inputName(Cube.java:40)
at Main.main(Main.java:88)

what's wrong?

2 Answers

Do not close your Scanner, it will close System.in as well.

When a Scanner is closed, it will close its input source if the source implements the Closeable interface

As I understand (correct me if I'm wrong) the reason why you close your objNumInput is that you want to use it in two different methods.

I would suggest you to pass the Scanner as input parameter into your methods inputName and inputLength. Then you'll be able to reuse the same scanner without closing it in between.

public static String inputName(Scanner scanner){
    String cubeName;
    System.out.println("Enter the name: ");
    cubeName = scanner.nextLine();
    return cubeName;
}

public static double inputLength(Scanner scanner){
    double cubeLength;
    System.out.println("Enter the length: ");
    cubeLength = scanner.nextDouble();
    return cubeLength;
}    

...

System.out.println("Enter the amount of objects you want to create");
Scanner objNumInput = new Scanner(System.in);
int objNum = objNumInput.nextInt();
//objNumInput.close(); <-- Do not close the scanner
Cube cubes[] = new Cube[objNum];

for (int i = 0; i < objNum; i++){
    String cubeName = Cube.inputName(objNumInput);
    double cubeLength = Cube.inputLength(objNumInput);
    cubes[i] = new Cube(cubeName, cubeLength);
}

put objNumInput.close(); after for loop in your main method.The reason your program flashes by without pausing the second time because System.in is closed when you do objNumInput.close(); in the line number 3 of main method

  • closing a Scanner object will close the underlying stream. -your code only works one time because System.in is getting closed. You cannot "open" System.in again. A closed stream cannot be reopened
Related