Java Scanner close() Method

The close() is a method of Java Scanner class which is used to closes this scanner.

Syntax

Following is the declaration of close() method:

Parameter

This method does not accept any parameter.

Returns

The close() method does not return any value.

Exceptions

NA

Compatibility Version

Java 1.5 and above

Example 1

Output:

Hi All! This is JavaTpoint.
Closing Scanner...
Scanner Closed.

Example 2

Output:

Enter Your Name: JavaTpoint
Name: JavaTpoint
Scanner Closed.

Example 3

Output:

Throws Exception If Number is of Type Long.
Enter your rollno: 345643985649356
Exception in thread "main" java.util.InputMismatchException: For input string: "345643985649356"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at myPackage.ScannerCloseExample3.main(ScannerCloseExample3.java:9)