Skip to content
Related Articles

Related Articles

Difference between next() and nextLine() methods in Java

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 27 Dec, 2021

The Scanner class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming. The scanner class consists next() and nextLine() methods. 
In this article, the difference between these two methods is discussed. 

next() Method: The next() method in java is present in the Scanner class and is used to get the input from the user. In order to use this method, a Scanner object needs to be created. This method can read the input only until a space(” “) is encountered. In other words, it finds and returns the next complete token from the scanner. 

The following is an example of how the next() method is implemented in Java: 

Java




// Java program to demonstrate
// the next() method
 
import java.util.Scanner;
 
class GFG {
    public static void main(String[] args)
    {
        // Creating the Scanner object
        Scanner sc = new Scanner(System.in);
 
        // Use of the next() method
        String Input = sc.next();
        System.out.println(Input);
    }
}

Input: 

Geeks for  
geeks

Output:  

Geeks

nextLine() Method: The nextLine() method in java is present in the Scanner class and is used to get the input from the user. In order to use this method, a Scanner object needs to be created. This method can read the input till the end of line. In other words, it can take input until the line change or new line and ends input of getting ‘\n’ or press enter. 
The following is an example of how the nextLine() method is implemented in Java:

Java




// Java program to demonstrate
// the nextLine() method
 
import java.util.Scanner;
 
class GFG {
    public static void main(String[] args)
    {
        // Creating the object of the
        // Scanner class
        Scanner sc = new Scanner(System.in);
 
        // Use of nextLine() method
        String Input = sc.nextLine();
        System.out.println(Input);
    }
}

Input: 

Geeks for  
geeks

Output:  

Geeks for 

The following table describes the difference between the next() and the nextLine() method:

Next()NextLine()
It read input from the input device till the space character. 
 
It read input from the input device till the line change. 
 
It cannot read those words having space in it. 
 
It can read those words having space in it. 
 
It ends reading the input after getting space. 
 
It ends reading the input after getting ‘\n’ or press enter. 
 
It places the cursor in the same line after reading the input. 
 
It places the cursor in the next line after reading the input. 
 
The escaping sequence of next() is space. 
 
The escaping sequence of nextLine() is ‘\n’. 
 
Syntax to scan input: 
Scanner.next() 
 
Syntax to scan input: 
Scanner.nextLine() 
 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!