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

System.out.print() does not print line substring up to carriage return

    XMLWordPrintable

Details

    • Not verified

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Found to be consistent on x64 Windows 10 with JDK 17 early access build (2021/4/7), JDK 16, JDK 11.0.1, JDK 1.8.0, and JDK 1.7.0 using any of the following consoles: Windows command prompt, Git Bash for Windows, and IntelliJ's console.

      A DESCRIPTION OF THE PROBLEM :
      If a String contains a carriage return in the middle of a line then the part of that line up to and including the carriage return is not printed.

      The following program

      ```
      public static void main(String[] args) {
      System.out.print("abc\r\ndef\rgh\njkl");
      }
      ```

      outputs the following:

      ```
      abc
      gh
      jkl
      ```

      This does not seem like desirable behavior, and it certainly caused me some confusion when I wanted to see several characters either side of a regular expression match but this caused a \r\n to be split and a significant portion of the string was not printed.

      It used to be more common for the carriage return to allow overprinting, but it's my understanding this was for "bold or accented characters, underscores, struck-out text, and some composite symbols" (https://www.asciihex.com/character/control/13/0x0D/cr-carriage-return), not wiping the whole line clean and starting afresh. It also does not appear to me to be a common function of the carriage return any longer.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Save the following to PrintBug.java:

      ```
      public class PrintBug {
      public static void main(String[] args) {
      System.out.print("abc\r\ndef\rgh\r\njkl");
      }
      }
      ```

      Compile it:

      ```
      > "path/to/java/javac.exe" PrintBug.java
      ```

      Run it:

      ```
      > "path/to/java/java.exe" PrintBug
      ```

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Output includes "def", for example:

      ```
      abc
      defgh
      jkl
      ```
      ACTUAL -
      abc
      gh
      jkl

      ---------- BEGIN SOURCE ----------
      public class PrintBug {
      public static void main(String[] args) {
      System.out.print("abc\r\ndef\rgh\r\njkl");
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Activity

          People

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: