Monday 6 March 2023

Scanner throws java.util.NoSuchElementException while reading the input

While reading the input using Scanner, I stuck with below error.

Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at com.sample.app.ScannerDemo.main(ScannerDemo.java:11)

 

My code snippet looks like below.

 

ScannerDemo.java
package com.sample.app;

import java.util.Scanner;

public class ScannerDemo {

	public static void main(String[] args) {
		while (true) {
			try (Scanner scanner = new Scanner(System.in)) {
				System.out.println("Enter a number");
				int userInput = scanner.nextInt();
				System.out.println("User entered : " + userInput);

				System.out.println("Enter non zero to continue and 0 to exit");

				userInput = scanner.nextInt();
				if (0 == userInput) {
					System.exit(0);
				}

			}
		}

	}

}

 

Sample Output

Enter a number
123
User entered : 123
Enter non zero to continue and 0 to exit
1
Enter a number
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at com.sample.app.ScannerDemo.main(ScannerDemo.java:11)

 

Root cause

After some analysis, I figured out there is a problem in using try-with-resource statement. try-with-resource statement close the scanner instance automatically when the scanner went out of scope. When the scanner#close is initiated, it internally close the System.in input stream as well.

 

I verified the same using below statement.

System.out.println("Is the input stream available ?" + System.in.available());

ScannerDemo.java

package com.sample.app;

import java.io.IOException;
import java.util.Scanner;

public class ScannerDemo {

	public static void main(String[] args) throws IOException {
		while (true) {
			try (Scanner scanner = new Scanner(System.in)) {
				System.out.println("Is the input stream available ?" + System.in.available());

				System.out.println("Enter a number");
				int userInput = scanner.nextInt();
				System.out.println("User entered : " + userInput);

				System.out.println("Enter non zero to continue and 0 to exit");

				userInput = scanner.nextInt();
				if (0 == userInput) {
					System.exit(0);
				}

			}

			System.out.println("Is the input stream available ?" + System.in.available());
		}

	}

}

Sample Output

Is the input stream available ?0
Enter a number
123
User entered : 123
Enter non zero to continue and 0 to exit
1
Exception in thread "main" java.io.IOException: Stream closed
	at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)
	at java.io.BufferedInputStream.available(BufferedInputStream.java:410)
	at com.sample.app.ScannerDemo.main(ScannerDemo.java:26)

How I resolved the problem?

Do not close the Scanner, as long as you need to work with it.

 

ScannerDemo.java

package com.sample.app;

import java.io.IOException;
import java.util.Scanner;

public class ScannerDemo {

	public static void main(String[] args) throws IOException {
		try (Scanner scanner = new Scanner(System.in)) {
			while (true) {

				System.out.println("Enter a number");
				int userInput = scanner.nextInt();
				System.out.println("User entered : " + userInput);

				System.out.println("Enter non zero to continue and 0 to exit");

				userInput = scanner.nextInt();
				if (0 == userInput) {
					System.exit(0);
				}

			}

		}

	}

}

Sample Output

Enter a number
123
User entered : 123
Enter non zero to continue and 0 to exit
1
Enter a number
124
User entered : 124
Enter non zero to continue and 0 to exit
1
Enter a number
125
User entered : 125
Enter non zero to continue and 0 to exit
0


 

You may like

Interview Questions

Quick guide to load balancers

Constant folding in Java

Quick guide to assertions in Java

java.util.Date vs java.sql.Date

How to break out of nested loops in Java?

Implementation of Bag data structure in Java

No comments:

Post a Comment