Why is Scanner skipping nextLine() after use of other next functions?

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()

What is the Issue with nextLine() method?



Consider the following code example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to show the issue with
// nextLine() method of Scanner Class
  
import java.util.Scanner;
  
public class ScannerDemo1 {
    public static void main(String[] args)
    {
        // Declare the object and initialize with
        // predefined standard input object
        Scanner sc = new Scanner(System.in);
  
        // Taking input
        String name = sc.nextLine();
        char gender = sc.next().charAt(0);
        int age = sc.nextInt();
        String fatherName = sc.nextLine();
        String motherName = sc.nextLine();
  
        // Print the values to check
        // if the input was correctly obtained.
        System.out.println("Name: " + name);
        System.out.println("Gender: " + gender);
        System.out.println("Age: " + age);
        System.out.println("Father's Name: "
                           + fatherName);
        System.out.println("Mother's Name: "
                           + motherName);
    }
}

chevron_right


  • When this code is run against the input:
    abc
    m
    1
    xyz
    pqr
    
  • Expected Outupt:
    Name: abc
    Gender: m
    Age: 1
    Father's Name: xyz
    Mother's Name: pqr
    
  • Actual Outupt:
    Name: abc
    Gender: m
    Age: 1
    Father's Name: 
    Mother's Name: xyz
    

As you can see, the nextLine() method skips the input to be read and takes the name of Mother in the place of Father. Hence the expected output does not match with the actual output. This error is very common and causes a lot of problems.

Why does this issue occur?
This issue occurs because, when nextInt() method of Scanner class is used to read the age of the person, it returns the value 1 to the variable age, as expected. But the cursor, after reading 1, remains just after it.

abc
m
1_ // Cursor is here
xyz
pqr

So when the Father’s name is read using nextLine() method of Scanner class, this method starts reading from the cursor’s current position. In this case, it will start reading just after 1. So the next line after 1 is just a new line, which is represented by ‘\n’ character. Hence the Father’s name is just ‘\n’.

How to solve this issue?
This issue can be solved by either of the following two ways:

  1. reading the complete line for the integer and converting it to an integer, or

    Syntax:

    // Read the complete line as String
    // and convert it to integer
    int var = Integer.parseInt(sc.nextLine());
    
  2. by consuming the leftover new line using the nextLine() method.

    Syntax:

    // Read the integer
    int var = sc.nextInt();
    
    // Read the leftover new line
    sc.nextLine();
    

Below example shows how to solve this issue with nextLine() method:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to solve the issue with
// nextLine() method of Scanner Class
  
import java.util.Scanner;
import java.io.*;
import java.lang.*;
  
class ScannerDemo1 {
    public static void main(String[] args)
    {
        // Declare the object and initialize with
        // predefined standard input object
        Scanner sc = new Scanner(System.in);
  
        // Taking input
        String name = sc.nextLine();
        char gender = sc.next().charAt(0);
  
        // Consuming the leftover new line
        // using the nextLine() method
        sc.nextLine();
  
        // reading the complete line for the integer
        // and converting it to an integer
        int age = Integer.parseInt(sc.nextLine());
  
        String fatherName = sc.nextLine();
        String motherName = sc.nextLine();
  
        // Print the values to check
        // if the input was correctly obtained.
        System.out.println("Name: " + name);
        System.out.println("Gender: " + gender);
        System.out.println("Age: " + age);
        System.out.println("Father's Name: "
                           + fatherName);
        System.out.println("Mother's Name: "
                           + motherName);
    }
}

chevron_right


  • When this code is run against the input:
    abc
    m
    1
    xyz
    pqr
    
  • Expected Outupt:
    Name: abc
    Gender: m
    Age: 1
    Father's Name: xyz
    Mother's Name: pqr
    
  • Actual Outupt:
    Name: abc
    Gender: m
    Age: 1
    Father's Name: 
    Mother's Name: xyz
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.