Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaDifference between next() and nextLine() methods in Java

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




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

 

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