Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Scanner's nextLine(), next(), and nextInt() methods

Tags:

java

I am trying to understand how these three methods work. Here's how I understood them:

  • nextLine() reads the remainder of the current line even if it is empty.
  • nextInt() reads an integer but does not read the escape sequence "\n".
  • next() reads the current line but does not read the "\n".

Suppose I have the following code:

import java.util.Scanner;

public class Welcome2
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Next enter two words:");

        int n; 
        String s1, s2; 

        n = keyboard.nextInt(); 
        s1 = keyboard.next(); 
        s2 =  keyboard.nextLine(); 
        System.out.println(" n is " + n + " s1 is " + s1 + " s2 is " + s2); 
    }
}

If my input is :

2 

Hi 

Hello 

Then I get the following output on screen:

n is 2 
s1 is hi 
s2 is  

Why would s1 have a value of "HI"?

Does this mean that the method next() reads the next line even though the escape character for the first line has not been read by nextInt()?

like image 513
mcfred Avatar asked Sep 26 '15 15:09

mcfred


People also ask

What is difference between next () and nextLine () nextInt?

Here's how I understood them: nextLine() reads the remainder of the current line even if it is empty. nextInt() reads an integer but does not read the escape sequence "\n". next() reads the current line but does not read the "\n".

What is next () and nextLine ()?

The nextLine() method returns all text up to a line break. The next() method returns tokenized text. The next() method uses whitespace as the default delimiter. The delimiter of the next() method can be changed to any valid String.

What is nextInt () method?

nextInt() The nextInt() method of a Scanner object reads in a string of digits (characters) and converts them into an int type. The Scanner object reads the characters one by one until it has collected those that are used for one integer. Then it converts them into a 32-bit numeric value.

What is next () method from scanner?

next() method finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.


2 Answers

If your input is 2 hello hi

nextInt() - just read the next token as a int (Here - it is 2) otherwise it give error

next() - Just read the next token, (here - it is hello)

nextline() - It read a line until it gets newline (here - after read previous 2 hello input by nextInt, next; nextline read hi only because after that it finds a newline )

if input is

2 

Hi 

Hello 

nextInt, next is same for above discussion .

In nextline(), it finds newline after completing the input Hi read by next(). So, nextline() stops to read input for getting the newline character.

like image 158
Animesh Kumar Paul Avatar answered Oct 14 '22 07:10

Animesh Kumar Paul


nextLine() reads the remainder of the current line even if it is empty.

Correct.

nextInt() reads an integer but does not read the escape sequence "\n".

Correct1.

next() reads the current line but does not read the "\n".

Incorrect. In fact, the next() method reads the next complete token. That may or may not be the rest of the current line. And it may, or may not consume an end-of line, depending on where the end-of-line is. The precise behavior is described by the javadoc, and you probably need to read it carefully for yourself in order that you can fully understand the nuances.

So, in your example:

  1. The nextInt() call consumes the 2 character and leaves the position at the NL.

  2. The next() call skips the NL, consumes H and i, and leaves the cursor at the second NL.

  3. The nextLine() call consumes the rest of the 2nd line; i.e. the NL.


1 ... except that your terminology is wrong. When the data is being read, it is not an escape sequence. It is an end-of-line sequence that could consist of a CR, a NL or a CR NL depending on the platform. The escape sequences you are talking about are in Java source code, in string and character literals. They may >>represent<< a CR or NL or ... other characters.

like image 25
Stephen C Avatar answered Oct 14 '22 09:10

Stephen C