Scanner nextLine() method in Java with Examples Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 18 Likes Like Report Try it on GfG Practice The nextLine() method of java.util.Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end. The next is set to after the line separator. Since this method continues to search through the input looking for a line separator, it may search all of the input searching for the line to skip if no line separators are present. Syntax: public String nextLine() Parameters: The function does not accepts any parameter. Return Value: This method returns the line that was skipped Exceptions: The function throws two exceptions as described below: NoSuchElementException: throws if no line was found IllegalStateException: throws if this scanner is closed Below programs illustrate the above function: Program 1: Java // Java program to illustrate the // nextLine() method of Scanner class in Java // without parameter import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { String s = "Gfg \n Geeks \n GeeksForGeeks"; // create a new scanner // with the specified String Object Scanner scanner = new Scanner(s); // print the next line System.out.println(scanner.nextLine()); // print the next line again System.out.println(scanner.nextLine()); // print the next line again System.out.println(scanner.nextLine()); scanner.close(); } } Output: Gfg Geeks GeeksForGeeks Program 2: To demonstrate NoSuchElementException Java // Java program to illustrate the // nextLine() method of Scanner class in Java import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { String s = ""; // create a new scanner // with the specified String Object Scanner scanner = new Scanner(s); System.out.println(scanner.nextLine()); scanner.close(); } catch (Exception e) { System.out.println("Exception thrown: " + e); } } } Output: Exception thrown: java.util.NoSuchElementException: No line found Program 3: To demonstrate IllegalStateException Java // Java program to illustrate the // nextLine() method of Scanner class in Java // without parameter import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { String s = "Gfg"; // create a new scanner // with the specified String Object Scanner scanner = new Scanner(s); scanner.close(); // Prints the new line System.out.println(scanner.nextLine()); scanner.close(); } catch (Exception e) { System.out.println("Exception thrown: " + e); } } } Output: Exception thrown: java.lang.IllegalStateException: Scanner closed Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine() Create Quiz Comment G gopaldave Follow 18 Improve G gopaldave Follow 18 Improve Article Tags : Java Java-Library Java - util package Java-Functions Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like