The class Scanner
has several methods with the term next*
, such as:
-
next()
fetches and returns the next TOKEN complete (returns: 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()
Parses the next TOKEN of an input in returns 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 entry that was "skipped" (returns: String
)
Forgetting nextLine()
, note that they all speak of a TOKEN such, "but what is TOKEN?", TOKEN there refers to something that is expected, in the example nextInt
expects something that was typed "case" with int
, so if you do this:
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 pressing Enter you type:
1 2 3
And then just hit Enter , note that it will already display the 3 System.out.println
, because both space and line break will be considered to separate values, and these values between 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 enter this all before pressing Enter :
1 2 ab
After pressing Enter, all 3% with% will again be displayed, so token all work by spaces as much as line breaks.
Now the System.out.println
My translation is 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 even did not understand the ignored one, it's not that the line was "ignored", the sense of skipped there is that it passed to the next line when you ran the method, ie it would be more for something like "return the entry that was from the line that was skipped" (I do not know if skipped sounds good, maybe I'll check the text).
So, in fact, the only thing that works with the entire line instead of TOKENs is
nextLine
, so it's as if the entire line was a token, to explain why I'd rather use your own code (read the comments).
Search for TOKEN (you do not need to match nextLine
, it can be anything other than a space), but stay on "line 1" :
int valor1 = entrada.nextInt();
We are still in line 1 , but every int
function will always work after the last TOKEN, so the next*
ignores, only the 1
or \n
of the typed input, so in this case it will return an empty%%, since line breaks are not values for TOKENs and will go to line 2 :
String texto = entrada.nextLine();
Now we are in line 2 and not in "line 3" , but in your line 2 you typed \r\n
, which does not "house" with the next command:
int valor2 = entrada.nextInt();
Then there is the exception String
, following the complete input
123
abc
456