views:

136

answers:

4
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().

+2  A: 

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.

relet
-1 for being wrong. It must be a carriage return (which goes back to the beginning of the line) and cannot be a line feed (which would drop down to the next line). These are throwbacks to the days of the typewriter when these were two separate actions. Carriage return was when you pushed the roll of paper back to the right so you could start on the first column again. Line feed was when you pushed in the little lever which fed the paper through by one line. On every typewriter I ever used these were engineered to happen at the same time.
Erick Robertson
I am fully aware of that. But without knowledge about the terminal the poster is using, there is always a chance that it is a broken line feed. There is nothing wrong in checking against both.
relet
+14  A: 

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");
        }
}

Output:

TEST this 
Kaleb Pederson
+1 for being right on the money.
Erick Robertson
This is arguably a problem with the elided functionality that reads in the message. It's likely not dealing with the IO format appropriately.
Alan Krueger
Maybe somewhere in the code someone is not using the standard system property `line.separator`: the platform-dependent line separator (e.g., "\n" on UNIX, "\r\n" for Windows)" Maybe the application was developed on *NIX, but now used on some Windows, with hardcoded \n instead of usage of line.separator property. So there is some \r remaining ...
Michael Konietzka
+1  A: 

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

Michael Konietzka
A: 

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

Alain O'Dea