Run this program in a Windows cmd.exe window:
import java.util.ArrayList;
public class A1 {
public static void main(String[] args) throws Exception {
System.console().readPassword("Password: ");
var list = new ArrayList<Integer>();
while (true) {
int x = System.in.read();
list.add(x);
if (x == 'z') break;
}
System.out.println(list);
}
}
After entering a random password, typing "1 2 3 4 Enter z Enter" will not end the program. Instead, I have to type "1 2 3 4 Enter x z Enter" for it to end, and it then outputs:
[49, 50, 51, 52, 13, 122].
It appears the "\r" is causing two subsequent characters -- "\n" and "x" -- to be effectively ignored.
This does not happen if "Console::readPassword" or "Console::readLine" is omitted at the start. It also does not happen in a Cygwin window. In Cygwin, there’s no "\r" character present, but calling a Console method has no impact on later System.in.read calls.
import java.util.ArrayList;
public class A1 {
public static void main(String[] args) throws Exception {
System.console().readPassword("Password: ");
var list = new ArrayList<Integer>();
while (true) {
int x = System.in.read();
list.add(x);
if (x == 'z') break;
}
System.out.println(list);
}
}
After entering a random password, typing "1 2 3 4 Enter z Enter" will not end the program. Instead, I have to type "1 2 3 4 Enter x z Enter" for it to end, and it then outputs:
[49, 50, 51, 52, 13, 122].
It appears the "\r" is causing two subsequent characters -- "\n" and "x" -- to be effectively ignored.
This does not happen if "Console::readPassword" or "Console::readLine" is omitted at the start. It also does not happen in a Cygwin window. In Cygwin, there’s no "\r" character present, but calling a Console method has no impact on later System.in.read calls.