I have a text file with 4 lines of data.
AAA,ZZZ,555,10
BBB,KKK,908977,5
CCCCC,WIKY PODAR,130000,15
DDDDD,XXXXX555,130110,30
then I read them and split them , remove the carrige return , replaced antoher coma and stored them into an Array.
Print out the arry , It looks fine :
[AAA, ZZZ, 555, 10, BBB, KKK, 908977, 5, CCCCC, WIKY PODAR, 130000, 15, DDDDD, XXXXX555, 130110, 30]
However, I printed out them indivisually, 10 and BBB became a single array element. How to sperate them into two different array elements?
Thank you.
output:
AAA
ZZZ
555
10, BBB
KKK
908977
5, CCCCC
WIKY PODAR
130000
15, DDDDD
XXXXX555
130110
30
while (inputStream.read(buffer) != -1) {
String testString2 = new String(buffer);
String delim2 = ",";
String[] token2 = testString2.split(delim2);
String[] myStringArray = new String[token2.length];
for (int i = 0; i < token2.length; i++) {
token2[i]=token2[i].replaceAll("[\n]", "");
token2[i]=token2[i].replaceAll("[\r]", ", ");
myStringArray[i] = token2[i];
}
Why not try the following way instead? Shorter and sweeter?
List<String> lines = Files.readLines(file, Charsets.UTF_8);
for(String line : lines) {
String[] words = line.split(",");
System.arrayCopy(words, 0, myStringArray, myStringArray.length, words.length);
}
However if you really want to do it the way you have, you need to
replace
token2[i]=token2[i].replaceAll("[\r]", ", ");
with
token2[i]=token2[i].replaceAll("[\r]", "");
There is no need to replace the carriage return with a comma.