Java.Lang.Illegalstateexception: Scanner Closed

java.lang.IllegalStateException: Scanner closed ERROR

You close the scanner and then call nextInt on it

 s.close(); //remove this line

calc c = new calc();
int a,b;
System.out.print("Enter the first number:");
a=s.nextInt();

So remove s.close(); and your code will work

Why do I get java.lang.IllegalStateException: Scanner closed

after you call input.close(); then the Scanner Object will be closed. no need to repeat, just once you call input.close();

Exception: Scanner closed how to re-open it within (while.. loop)

There are two solutions:

  • Don't close the scanner. Keep it open until you don't need it. In other words, close it after the loop.
  • Recreate the scanner by calling sc = new Scanner(new File("addressBook.txt"));. However, since this will create a new scanner, it'll start reading from the first line again.

IllegalStateException: Scanner closed and Ending application program flow

  System.out.println("Age: ");

person1.setAge(Integer.parseInt(string_input.nextLine()));

System.out.println("Gender: ");

gender = string_input.nextLine().charAt(0);

Always use nextLine because, when you use Scanner.nextInt() and then call Scanner.nextLine(), it will return the remaining part of the line from the line where you read the int which basically is an empty string.

Javadoc of nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Run Error in Java...Scanner closed?

Replace inputFile with inputFile2 because it's the Scanner object.

Java IntToBinary app: Exception in thread main java.lang.IllegalStateException: Scanner closed

There are two issues with your code. One you've already identified, which is trying to read from an already closed Scanner. To fix it simply return from enterAnotherInt() right after your call to AnotherOrCloseIntToBinary.goodBye() as

     switch (state) 
{
case 0:
System.out.println();
System.out.println("Do you want to enter another integer number? (y/n)");
break;
case 1:
AnotherOrCloseIntToBinary.goodBye();
return; // instead of break;
}

The second issue that you haven't realised yet is the in.nextInt() call within your while loop.

    while ((!in.hasNextInt())) 
{
System.out.print("You did not enter an integer number. An integer number(whole numbers or counting numbers) is a number like: 1, 2, 3...");
System.out.print("Please enter an integer number: ");
in.nextInt(); // use in.next() here
}

To see how this fails simply enter a non-numeric input when your code prompts for an integer. You would receive an InputMismatchException. The reason it fails is that you're already in the loop because the input in not a number, yet your code goes ahead and fires a nextInt() call on the scanner which is bound to fail then.

To fix it just use in.next() instead of nextInt() there.



Related Topics



Leave a reply



Submit