Bizzare System.out.println () in a Java program

String messageFile = ... // Assume messageFile SHOULD have the string "MESSAGE"
System.out.println("The messageFile is: " + messageFile + "!!");

You can usually expect the above command to be output:

The messageFile is: MESSAGE!!!!

However, instead I get this:

!!e messageFile is: MESSAGE

See above as "!!" the dots seem to wrap the message. My theory is this:

String messageFile = ... 

contains more characters than my intended "MESSAGE". As a result, it transfers the next input (in this case, "!!") to the beginning of the System.out.println () message.

What character causes this?

Additional Information:

Btw, messageFile is initialized by passing a command line argument to the java myClassA class. The myClassA constructor uses super () to pass the messageFile parameter to myClassB. myClassB passes messageFile to the () function.

+3
4

, (\r) messageFile, (\n).

EDIT - :

class Println {
        public static void main(String[] args) {
                System.out.println("xxxx this \rTEST");
        }
}

:

TEST this 
+16

, , "\ r" ( ) "\n" () . , .

+2

messageFile codePointAt. messageFile.

+1

, :

messageFile.replace('\r', '\n').replace("\n\n", "\n)

Carriage return must be prohibited: D

0
source

Source: https://habr.com/ru/post/1758036/


All Articles