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

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



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 Inpt = sc.next();
        System.out.println(Inpt);
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 Inpt = sc.nextLine();
        System.out.println(Inpt);
    }
}

chevron_right


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

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




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.