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

New line character not working on logic statements

XMLWordPrintable

    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      Microsoft Windows [Versione 10.0.18362.535]

      openjdk 11 2018-09-25
      OpenJDK Runtime Environment 18.9 (build 11+28)
      OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      There is a common rule that instruct programmers to use '\n' character to inform the compiler that the line should terminate; it is responsibility of the compiler translate those character to the most appropriate for the Operating System.

      Well, this translation works on strings or something that have to go to an output, but does't works for input of characters, exspecially where (like Windows) the new line is represented by two characters: '\r'+'\n'; if a programmer wrote a program like the following, it will not works:
      __________________________________________________________________________________________________________________

      char inputChar = stream.read();

      if(inputChar == '\n')
          System.out.println("This is a new line character");
      __________________________________________________________________________________________________________________

      Because the compiler translate the '\n' with the more appropriate, but on Windows, there are two of them! Therefore only one character pass the condition, the other one, will go as a normal character, but will produce for sure fatal errors on the logic that will use the new line character.

      This is a millennium bug that will limit the platform independece of Java, but I am sure it is not only Java, so if the reader of this text, have as mission the good and interoperability of software, it is his or her responsibility to inform the entire programming comunity, especially those that build platform independent languages and softwares.


      ---------- BEGIN SOURCE ----------
      InputStream stream = null;//load a text file that have new line chars on Windows OS

      char inputChar = 0;

      while((inputChar = stream.read()) >= 0)
      {
          if(inputChar == '\n')
              System.out.println("This is a new line character with code: " + ((int) inputChar));
          else
              System.out.println("This is a character with code: " + ((int) inputChar));
      }

      stream.close();
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The expression: "inputChar == '\n'" should be expanded by the compiler to test all the characters that are used for new line, on Windows: inputChar == '\n' || inputChar == '\r'.

      I don't know the inner working of the JVM and of the bytecode, but I suggest a dedicated instruction for that purpose, that the VM compiled for that platform will interpret for that specific platform... As a hotfix, you can replace the expression with a method call placed by the sorce code compiler on the class executing the code (as I did in my app).

      New paragraphs, therefore new texts need new lines: Let's be sure that our software understand that!

      FREQUENCY : always


        1. test.java
          0.5 kB
        2. Test1.java
          19 kB

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

              Created:
              Updated:
              Resolved: