Scanner close() method in Java with Examples Read Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report The close() method ofjava.util.Scanner class closes the scanner which has been opened. If the scanner is already closed then on calling this method, it will have no effect. Syntax: public void close() Return Value: The function does not return any value. Below programs illustrate the above function: Program 1: // Java program to illustrate the // close() method of Scanner class in Java import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Get the string String s = "Geeksforgeeks has Scanner Class Methods"; // 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: " + scanner.nextLine()); // close the scanner scanner.close(); System.out.println("\nScanner Closed.\n"); System.out.println("Trying to use scanner" + " after closing."); // print the next line of the string System.out.println(scanner.nextLine()); } catch (Exception e) { System.out.println("Exception thrown:\n" + e); } } } Output: Scanner: Geeksforgeeks has Scanner Class Methods Scanner Closed. Trying to use scanner after closing. Exception thrown: java.lang.IllegalStateException: Scanner closed Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close()Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer: Comprehensive Course Expert Guidance for Efficient Learning Hands-on Experience with Real-world Projects Proven Track Record with 100,000+ Successful Geeks Last Updated : 10 Oct, 2018 Like Article Save Article Previous GregorianCalendar clone() Method in Java Next TreeSet floor() method in Java with Examples Share your thoughts in the comments Add Your Comment Please Login to comment...