Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8348590

Console calls have strange impact on later System.in.read

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • None
    • windows

      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.

            jlahoda Jan Lahoda
            weijun Weijun Wang
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: