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()
?
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".
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.
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.
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.
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.
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:
The nextInt() call consumes the 2
character and leaves the position at the NL
.
The next() call skips the NL
, consumes H
and i
, and leaves the cursor at the second NL.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With