开发者

Bizzare System.out.println() in Java Program

String messageFile = ... // Assume messageFile SHOULD have the string "MESSAGE"
System.out.println("The messageFile is开发者_如何学C: " + 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");
        }
}

Output:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜