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 Inpt = sc.next(); System.out.println(Inpt); } } |
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 Inpt = sc.nextLine(); System.out.println(Inpt); } } |
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.
Recommended Posts:
- Why is Scanner skipping nextLine() after use of other next functions?
- Scanner nextLine() method in Java with Examples
- Static methods vs Instance methods in Java
- Difference between Next Generation Network and Traditional Network
- Difference between Traditional Firewall and Next Generation Firewall
- Difference between the Constructors and Methods
- Differences between wait() and join() methods in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.io.BufferedWriter class methods in Java
- Differentiate between Write Through and Write Back Methods
- Quick ways to check for Prime and find next Prime in Java
- Private and final methods in Java
- ArrayList and LinkedList remove() methods in Java with Examples
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Java.lang.Integer class and its methods
- java.math class and its methods | Set 1
- java.math class and its methods | Set 3
- getproperty() and getproperties() methods of System Class in Java
- equals() and hashCode() methods in Java
- Java Concurrency - yield(), sleep() and join() methods
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.

