The class Scanner
has several methods with the term next*
, as being:
next()
search and return the next complete TOKEN (return: String
)
nextBigDecimal()
Scans the next TOKEN of an input and returns as BigDecimal
nextBigInteger()
Scans the next TOKEN of an input and returns as BigInteger
nextBoolean()
Analyzes the next TOKEN of an input in returns in a value boolean
nextByte()
Scans the next TOKEN of an input and returns as byte
nextDouble()
Scans the next TOKEN of an input and returns as double
nextFloat()
Scans the next TOKEN of an input and returns as float
nextInt()
Scans the next TOKEN of an input and returns as int
nextLong()
Scans the next TOKEN of an input and returns as long
nextShort()
Scans the next TOKEN of an input and returns as short
nextLine()
Advance this scanner beyond the current line and return the input that was "skipped" (returns: String
)
Forgetting the nextLine()
, note that they all speak of such a TOKEN, "but what is the TOKEN?", TOKEN there refers to something that is expected, in the example nextInt
expects something that was typed to "marry" the int
, then if you do it:
Scanner entrada = new Scanner(System.in);
int valor1 = entrada.nextInt();
int valor2 = entrada.nextInt();
int valor3 = entrada.nextInt();
System.out.println("Retornou:" + valor1);
System.out.println("Retornou:" + valor2);
System.out.println("Retornou:" + valor3);
But instead of squeezing Enter you type:
1 2 3
And then only then to tighten the Enter, note that it will already display the 3 System.out.println
, this because both space and line break will be considered to separate the values, and these values between the separations are the TOKENS, whatever the value.
Now change to this:
Scanner entrada = new Scanner(System.in);
String valor1 = entrada.next();
String valor2 = entrada.next();
String valor3 = entrada.next();
System.out.println("Retornou:" + valor1);
System.out.println("Retornou:" + valor2);
System.out.println("Retornou:" + valor3);
And type all this before you press the Enter:
1 2 ab
After pressing Enter, again all 3 will be displayed System.out.println
, then token in all work for spaces as much as break lines.
Now the nextLine
My translation got a little bad, the original text of the documentation is this:
Advances this scanner Past the Current line and Returns the input that was skipped.
I think this text is what confuses a lot of people, when translating skipped the first time I myself did not understand the ignored, is not that the line was "ignored", the meaning of skipped there is that went to the next line when executed the method, ie it would be more for something like "returns the entry that was the line that was skipped" (I do not know if skipped sounds good, maybe I review the text).
So actually the only one who works with the entire line instead of the Tokens is the nextLine
, ie it is as if the entire line is a token, to explain better I will use your own code (read the comments).
Find the TOKEN (you don’t have to marry int
, may be anything other than a space), but remains in the "line 1":
int valor1 = entrada.nextInt();
We’re still in the "line 1", but every function next*
will always work after the last TOKEN, then the 1
you ignore, only leaves the \n
or \r\n
input, then in case it will return a String
empty, since line breaks are not values for Tokens and will pass to the "line 2":
String texto = entrada.nextLine();
Now we’re in the "line 2" and not in the "row 3", but in his "line 2" you had typed abc
, what not "home" with the next command:
int valor2 = entrada.nextInt();
Then occurs the a Exception InputMismatchException
, following the complete input
123
abc
456
Something tells me it’s duplicate, but I couldn’t find it.
– Maniero
I agree. I’ve answered that three times.
– Victor Stafusa
But I will not close as duplicate because in this one, this is the core of the problem, and not something tangential, allowing then a canonical answer.
– Victor Stafusa
I was going to close ;)
– Maniero
Just to explain otherwise, next, nextInt, nextLong, and others look for a specific "TOKEN" (regardless of the number of characters), nextLine looks for a line (in the case the entire line until the break is the token for nextLine), so the "problem occurs".
– Guilherme Nascimento