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();
}
everything = "Indonesian\nid\nYiddish\nyi"then it works. However,everythingis read from a txt file, the contents of which are here: pastebin.com/Tjf9dzcbeverything.split(System.lineSeparator())instead of\n.