java.lang.IllegalStateException: Scanner closed

Don’t call eingabeMove.close(); at the end of that while loop. You’re causing the Scanner to become inoperable at the end of the first loop.

Since the loop always terminates with a return, it doesn’t make sense to close the Scanner in this schleife() method.

You actually don’t need to close the Scanner though, because it wraps System.in which never closes anyhow. Given this fact, you can simply let eingabeMove go out of scope when the schleife() returns.

If you really want to close the Scanner, you should pass eingabeMove as a parameter to the method, and close it from the calling method.

public boolean schleife(Scanner eingabeMove) {
   // use the scanner
}

Calling code:

Scanner eingabeMove = new Scanner(System.in);
schleife(eingabeMove);
eingabeMove.close();

Leave a Comment