Scanner nextLine() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output:

Gfg 
 Geeks 
 GeeksForGeeks

Program 2: To demonstrate NoSuchElementException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Exception thrown: java.util.NoSuchElementException: No line found

Program 3: To demonstrate IllegalStateException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Exception thrown: java.lang.IllegalStateException: Scanner closed

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.