Fix Java Scanner NextLine Skips

Shikha Chaudhary Jan 09, 2022
Fix Java Scanner NextLine Skips

While using the nextLine() method after the next() or nextInt() method in Java, sometimes the nextLine() method skips the current input to be read and goes to the next input. This article will discuss why this happens and how to fix this issue.

Before we look into the issue with the nextLine() method, let us first understand the working of the nextLine() method. This method belongs to the java.util.Scanner class.

This function prints the entire line except for the line separator at the end. It searches for an input looking for a line separator. If no line separator is found, it may search all the input while searching for the line to skip.

This scanner method goes past the current line. If there is any input that was skipped, it returns it. Follow this link to refer to the method’s documentation.

Syntax:

public String nextLine()

Note that this function takes no parameter and returns the skipped line. Look at the example below to see how it works.

//To demonstrate the working of nextLine() method

import java.util.*;

public class Example{
    public static void main(String[] args)
    {
        String a = "Java \n is \n intriguing";
        
        //create a new scanner
        Scanner sc = new Scanner(a);
        
        //print the next line
        System.out.println(sc.nextLine());
        
        //print the next line again
        System.out.println(sc.nextLine());
        
        //print the next line again
        System.out.println(sc.nextLine());
        
    }
}

Output:

Java
 is
 intriguing

The main use of the nextLine() method in Java is to read a string input with space.

Note that we can also use the next() method to input a string, but it only reads up to the first space it encounters. Also, the next() method puts the cursor in the same line after taking the input.

However, the nextLine() method puts the cursor in the next line after reading the input. Look at the below example.

import java.util.*;
public class Demo{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        //Try reading a string with space using next()
        System.out.println("Enter the name of a movie");
        String name = sc.next();
        
        //Done to bring the cursor to next line
        sc.nextLine(); 
        
        //Re-reading the string using nextLine()
        System.out.println("Re-enter this name");
        String rename = sc.nextLine();
        
        System.out.println();
        System.out.println("Here is the output:");
        
        //printing the output
        System.out.println(name);
        System.out.println(rename);
    }
}

Output:

Enter the name of a movie
Harry Potter
Re-enter this name
Harry Potter

Here is the output:
Harry
Harry Potter

Note that the next() method only reads the first name before space and ignores the rest. However, the nextLine() method reads the entire string with space.

the Problem With the nextLine() Method

To understand this issue, look at the below example.

import java.util.Scanner;
public class Demo{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        //This part takes the input
        System.out.println("Input the Roll Number:");
        int number = sc.nextInt();
        System.out.println("Input your full name:");
        String name = sc.nextLine();
        System.out.println("Input your branch:");
        String branch = sc.nextLine();
        
        //This part prints the values 
        System.out.println("Roll No: "+ number);
        System.out.println("Name: "+ name);
        System.out.println("Branch: "+ branch);
        
    }
}

Output:

Input the Roll Number:
1
Input your full name:
Input your branch:
Computer Science
Roll No: 1
Name:
Branch: Computer Science

But the required output is this:

Input the Roll Number:
1
Input your full name:
Harry Potter
Input your branch:
Computer Science
Roll No: 1
Name: Harry Potter
Branch: Computer Science

Notice that the nextLine() method skips the input for Name and instead goes to the next input for the branch.

Why Does This Problem Occur

This happens because when the nextInt() method of the Scanner class reads the roll number, it returns the value 1. This value is stored in the variable number.

But the cursor does not go to the next line after reading the roll number. It remains just after 1. Like this:

1_ // the cursor remains here only

Therefore, when we use the nextLine() method to read the name, it starts reading the input from the cursor’s current position. So after 1, the next line is nothing but the new line itself. The \n character represents this new line. Thus, the name is just \n.

How to Fix This Problem of Java Scanner NextLine Skips

There are two ways in which we can fix this issue.

Using the Java Integer.parseInt() Method

The parseInt() method belongs to the Integer class of the java.lang package and returns the primitive data type of a certain string. These methods are of two types mainly:

  • Integer.parseInt(String s)
  • Integer.parseInt(String s, int radix)

To work with the nextLine() skips, we will use the first method - Integer.parseInt(String s). This reads the complete line for the Integer as String. Later, it converts it to an integer.

Syntax:

int value = Integer.parseInt(sc.nextLine());

Let us implement the same code we used above with the Java Integer.parseInt() method and rectify the problem.

import java.util.Scanner;
public class Demo{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        //This part takes the input
        System.out.println("Input the Roll Number:");
        
        // Using the parseInt() method this time
        int number = Integer.parseInt(sc.nextLine());
        System.out.println("Input your full name:");
        String name = sc.nextLine();
        System.out.println("Input your branch:");
        String branch = sc.nextLine();
        
        //This part prints the values 
        System.out.println("Roll No: "+ number);
        System.out.println("Name: "+ name);
        System.out.println("Branch: "+ branch);
        
    }
}

Output:

Input the Roll Number:
1
Input your full name:
Harry Potter
Input your branch:
Computer Science
Roll No: 1
Name: Harry Potter
Branch: Computer Science

This time the code runs fine. But we cannot use this for input string after Byte character(Byte.parseByte(sc.nextLine())).

We can use the second method in that case. Here is the link to documentation for more information.

Using an Extra nextLine() Statement

This consumes the leftover new line. Look at the example.

import java.util.Scanner;
public class Demo{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        //This part takes the input
        System.out.println("Input the Roll Number:");
        int number = sc.nextInt();
        
        //An extra nextLine() to consume the leftover line
        sc.nextLine();
        
        System.out.println("Input your full name:");
        String name = sc.nextLine();
        System.out.println("Input your branch:");
        String branch = sc.nextLine();
        
        //This part prints the values 
        System.out.println("Roll No: "+ number);
        System.out.println("Name: "+ name);
        System.out.println("Branch: "+ branch);
        
    }
}

Output:

Input the Roll Number:
1
Input your full name:
Harry Potter
Input your branch:
Computer Science
Roll No: 1
Name: Harry Potter
Branch: Computer Science

Related Article - Java Error

Related Article - Java Scanner