Tutorial :Bizzare System.out.println() in Java Program


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

Normally, one would expect the above command to output:

The messageFile is: MESSAGE!!!!  

However, I am receiving this instead:

!!e messageFile is: MESSAGE  

See how the above statement, the "!!" points seem to wrap around the message. My theory is that the:

String messageFile = ...   

contains more characters than my assumed "MESSAGE". As a result, it's wrapping the next input (in this case, the "!!") to the front of the System.out.println() message.

What character is causing this?

Extra info:

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


I would guess you have a stray carriage return (\r) within the messageFile variable that is unaccompanied by a line feed (\n).

EDIT - this tests as expected:

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


TEST this   


Your message variable possibly contains a '\r' (carriage return) or '\n' (line feed) character at the end. This may cause the cursor to return to the first column before printing the exclamation marks.


For debugging you should print the codepoint of each character of messageFile via codePointAt. As as result you see exactly the content of messageFile.


Replace all carriage returns in the file with newlines and then replace all double-newlines with single-newlines:

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

Carriage returns should be banned :D

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »