Home »
Java programming language
Java Scanner close() Method with Example
Scanner Class close() method: Here, we are going to learn about the close() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on February 18, 2020
Scanner Class close() method
- close() method is available in java.util package.
- close() method is used to close this Scanner object when opened otherwise this method does not affect.
- close() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- close() method does not throw an exception at the time of closing Scanner.
Syntax:
public void close();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is void, it returns nothing.
Example:
// Java program to demonstrate the example
// of void close() method of Scanner
import java.util.*;
public class CloseScanner {
public static void main(String[] args) {
String str = "Hi, IncludeHelp";
// Instantiates a Scanner object with
// the given string str
Scanner sc = new Scanner(str);
System.out.println("sc.nextLine(): " + sc.nextLine());
// By using close() method is to
// close the Scanner object
sc.close();
}
}
Output
sc.nextLine(): Hi, IncludeHelp