2

I have some basic code that is supposed to format an input string. The input string has a list of words, separated with a newline. My code is supposed to add to add a few chars to the beginning and end of each line. ("\"-" in the beginning and "\"," in the end.) However, although each element in the list is printed correctly when printing separately, the var 'out' does not contain all the elements, instead it contains "\",\"".

String[] split = everything.split("\n");
    String out = "\"";
    for (String split1 : split) {
        System.out.println(split1);
        out = out + "-" + split1.toLowerCase() + "\",\"";
    }
    System.out.println(out);

For example, for the input string:

Indonesian\nid\nYiddish\nyi

prints:

Indonesian
id
Yiddish
yi
","

when it should print:

Indonesian
id
Yiddish
yi
"-indonesian","-id","-yiddish","-yi","

Can someone explain what is causing this behavior and how to fix it?

Update: I did some more testing. It seems if i simply set everything to Indonesian\nid\nYiddish\nyithen the desired output comes out. However, everything is read from a big text file. I pasted the contents of the file here: http://pastebin.com/Tjf9dzcb

I read the file like this:

BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\xxxx\\Desktop\\hi.txt"));
    String everything = null;
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
        everything = sb.toString();
    } finally {
        br.close();
    }
8
  • Nope, java is not broken, see ideone.com/HFPsNd Commented Sep 19, 2015 at 20:27
  • +fge Sorry for the confusion. The last line of the output should be all lowercase. (The other lines should not) Commented Sep 19, 2015 at 20:28
  • Even if your 'everything' string is empty, I fail to see how you can get the output you mention. Are you sure this is all of the code? Commented Sep 19, 2015 at 20:33
  • +fge I did some more testing. It seems if i simply do everything = "Indonesian\nid\nYiddish\nyi" then it works. However, everything is read from a txt file, the contents of which are here: pastebin.com/Tjf9dzcb Commented Sep 19, 2015 at 20:37
  • 2
    Try changing the split to everything.split(System.lineSeparator()) instead of \n. Commented Sep 19, 2015 at 20:40

1 Answer 1

3

Use both sb.append(); and everything.split() with same parameter.

sb.append("\n");
String[] split = everything.split("\n");

Or

sb.append(System.lineSeparator());
String[] split = everything.split(System.lineSeparator());

EDIT

Definition of System.lineSeparator() in JavaDoc

Returns the system-dependent line separator string. It always returns the same value - the initial value of the system property line.separator. On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".

So if your system is Windows then System.lineSeparator() is equivalent to "\r\n". In UNIX your previous code should work well.

Sign up to request clarification or add additional context in comments.

4 Comments

Yup. Or change the input routine: sb.append('\n');
This somehow worked :D I however, don't understand how. Does '\n' != System.lineSeparator()? If so how was i able to use a for-each on the output of .split()?
Yes for Windows but No for UNIX. It's better to use System.lineSeparator() when needed.
On Windows, System.lineSeparator() == "\r\n". So there was a '\n' in there for the split to use. But there was that extra '\r'. That's a Carriage Return. (The '\n' is a Line Feed.) And Carriage Return means "move the cursor all the way to the left". There's your hint, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.