Not everything on the Internet is true or real.
Despite being quite widespread on the internet the use of class instances java.util.Scanner
as a substitute for the use of the method java.io.Console.readLine()
it’s not like that.
The class java.util.Scanner
is a text scanner that can parse primitive types and strings using regular expressions.
java.util.Scanner
divides your input into tokens using a standard delimiter, which initially corresponds to blanks.
To documentation says:
For simple line-oriented read-only applications, use java.io.Console.readLine()
That is to say java.util.Scanner
is a tool used to perform lexical analysis of a text coming from an object in the classes File
, InputStream
, Readable
, String
, ReadableByteChannel
or Path
.
In case the console read is passed System.in
an instance of InputStream
.
I wanted to understand why java.util.Scanner.nextLine()
does this and how to use it in this case?
The method nextLine()
is not a proper method for reading a character line. It is method for reading surplus characters, whether characters do not form tokens or characters discarded during lexical analysis. nextLine()
from the last token read and returns the remainder of the input that was ignored. As during the first run of the loop still no token was read by the scanner, nextLine()
blocks the program while waiting for entry, but in the second iteration of the loop a token had been read by java.util.Scanner.next()
and then nextLine()
from the last token only reads an empty line.
A repair possibility for your code is to use precedes the use nextLine()
with the method java.util.Scanner.hasNextLine()
true return if there is a line in the scanner entry, block and wait while waiting for an entry.
import java.util.*;
class Main {
public static void main(String args[]) {
Scanner teclado = new Scanner(System.in);
String nome="", senha="";
do {
System.out.println("Digite nome:");
if(teclado.hasNextLine()){
nome = teclado.nextLine();
}
System.out.println("Digite senha:");
if(teclado.hasNextLine()){
senha = teclado.nextLine();
}
if (nome.equals(senha)) {
System.out.println("Nome deve ser diferente de senha, Digite novamente");
}
} while (nome.equals(senha));
}
}
A proportional alternative task would be to use java.util.Console.readLine()
import java.io.*;
class Main {
public static void main(String args[]) {
Console teclado = System.console();
String nome="", senha="";
do {
System.out.println("Digite nome:");
nome = teclado.readLine();
System.out.println("Digite senha:");
senha = teclado.readLine();
if (nome.equals(senha)) {
System.out.println("Nome deve ser diferente de senha, Digite novamente");
}
} while (nome.equals(senha));
}
}
See if I got it, so if I envy password = keyboard.next(); put password = keyboard.nextLine(); it’s going right?
– p.h_compilado
It’ll only work if you put
if(teclado.hasNextLine()){nome = teclado.nextLine();}
andif(teclado.hasNextLine()){senha = teclado.nextLine();}
. The key to correct operation is the use ofteclado.hasNextLine()
that prepares the scanner to consume a line withteclado.nextLine()
.– Augusto Vasques
As to the
teclado.next()
if for example the user enters witha b c
,teclado.next()
will only reada
– Augusto Vasques