Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaWhy is Scanner skipping nextLine() after use of other next functions?

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: 

Java




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


  • When this code is run against the input
abc
m
1
xyz
pqr
  • Expected Output: 
Name: abc
Gender: m
Age: 1
Father's Name: xyz
Mother's Name: pqr
  • Actual Output: 
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());

Although this method is not applicable for input String after Byte character(Byte.parseByte(sc.nextLine()). Second method is applicable in that case.

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: 

Java




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


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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments