-4

I am writing a function to capitalize the first letter of each line of a string. So the following input:

String test="this is a test" + System.lineSeparator() +
                    "write some words" + System.lineSeparator() +
                    "and sentences";

output should be:

String expected="This is a test" + System.lineSeparator() +
                    "Write some words" + System.lineSeparator()+
            "And sentences";

However, when I run with Eclipse. The output is:

This is a test[
Write some words]
And sentences

Its byte:

54686973206973206120746573740a577269746520736f6d6520776f726473416e642073656e74656e636573

The expected string in Eclipse is:

This is a test[
Write some words
]
And sentences

Its byte:

54686973206973206120746573740d0a577269746520736f6d6520776f7264730d0a416e642073656e74656e636573

So two strings are not equal. I run some code in an online Java compiler, two strings are equal.

I am wondering if any specific system command should be set to accomodate with lineSeparator() and make two Strings (output and expected) be equal?

Thanks;

kaneroy
  • 3
  • 3
  • your question is not clear, what is your problem? what are you trying to achieve ? – Arun Xavier Nov 15 '17 at 05:03
  • Welcome to SO. Please take a minute to read [How to ask a question.](https://stackoverflow.com/help/how-to-ask) As it stands, you are currently not providing enough information to elicit a proper solution. – yacc Nov 15 '17 at 05:09
  • Your run configuration might contain a VM argument like `line.separator=\n`(see _Arguments_ tab of your run configuration). – howlger Nov 15 '17 at 07:42

1 Answers1

0

The issue you should be solving is not really with Eclipse. Your code should be written so that it is platform independent.

For your command-line/linux environment, the line separator is a line-feed (LF) often referred to as a new-line (NL; "\n"; '0a'). For Eclipse/Windows/DOS, the separator is carriage-return, line-feed (CRLF; "\r\n"; {'0d','0a'}).

Based on you hex output, it shows the Eclipse line separator is indeed CRLF, while the edited string you constructed only has LF; i.e. you are doing something that incorrectly changes the CRLF of the test string to a LF.

For example, you may be splitting the test string into multiple strings and then constructing the edited string with "\n"; e.g. str1 + "\n" + str2 -OR- String.format("%s\n%s", str1, str2). Instead, you should construct it with the System.LineSeperator(); e.g. String.format("%s%s%s", str1, System.LineSeperator(), str2).

If you really wish to change the Eclipse behavior, as I mentioned before, I'd suggest looking here.


REFERENCE: We don't see your code used to create the updated text. But you might try dumping the bytes of your two strings to see how they differ. Also take a look here.

For example, in Eclipse I see a carriage return and line feed.


    false
    4162630a446566
    4162630d0a446566


    public static void main(String args[])
    {
       String s = "Abc\nDef";
       String t = "Abc"+System.lineSeparator()+"Def";
       System.out.println(s.equals(t));
       System.out.println(hexStr(s.getBytes()));
       System.out.println((hexStr(t.getBytes())));
    }
    static String hexStr(byte[] bytes) {
        try (Formatter formatter = new Formatter()) {
            for (byte b : bytes) formatter.format("%02x", b);
            return formatter.toString();
        }
    }

robertf
  • 237
  • 1
  • 11
  • Thanks for your suggestion. I have updated the bytes in my questions now. – kaneroy Nov 15 '17 at 05:35
  • Your expected string has a carriage return (0d) and line feed (0a) as I would expect. Your edited string only has a line feed (0a). Thus, the way you are creating the edited string appears to be handling lineSeparators differently. Do you have more details on how you process the edit string? – robertf Nov 15 '17 at 05:46
  • Yes, that is what I find out (my Eclipse may handle lineSeparators differently). The way I process the string is very simple: for the test string, I read line by line. For each line, capitalize the first letter. Sorry, I cannot paste all the code here as this is a homework. Thanks for your help! – kaneroy Nov 15 '17 at 05:52
  • Some friend suggests me using intellij and looks it can handle the lineSeparator correctly and make two strings equal. However, if possible, I would like know why Eclipse (Windows environment) may handle this operator differently. Thanks! – kaneroy Nov 15 '17 at 05:56
  • How is the lineSeparator placed into the edited string? Based on your post, the test string is defined using the same System.LineSeparator() call; thus, I would expect the test string to also have CR/LF in it. Thus, it comes down to how you are processing the test string to create the edited string. For example, if you use "\n" to create the edit string, then you will only get a LF (0a). It is difficult to assist further without seeing the details of how you create the edit string. – robertf Nov 15 '17 at 05:59
  • In your comment above, you indicate "for the test string, I read line by line". This indicates to me that you are consuming the LineSeparator. My guess is that your logic to re-assemble the edited lines is not using System.LineSeparator() to rebuild the edited string (when you should be). – robertf Nov 15 '17 at 06:17
  • Thanks a lot for your reply. I think I find out the problem. As you post above, I use "\n" at the end of each line of a string as many other input strings also use "\n" and this is the only string which ends up with lineSeparator(). Thus, for this special input, I should replace "\n" with lineSeparator. Thanks a lot for your detail explanation! – kaneroy Nov 15 '17 at 17:26