Java.util.Scanner.close() Method
Advertisements
Description
The java.util.Scanner.close() method closes this scanner.If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.
Declaration
Following is the declaration for java.util.Scanner.close() method
public void close()
Parameters
NA
Return Value
This method does not return any value.
Exception
NA
Example
The following example shows the usage of java.util.Scanner.close() method.
package com.tutorialspoint;
import java.util.*;
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! 3+3.0=6";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// print the next line of the string
System.out.println("" + scanner.nextLine());
// close the scanner
System.out.println("Closing Scanner...");
scanner.close();
System.out.println("Scanner Closed.");
}
}
Let us compile and run the above program, this will produce the following result:
Hello World! 3+3.0=6 Closing Scanner... Scanner Closed.