Nagendra V
Nagendra V

Reputation: 137

I am trying to read a csv file in java. Below is my code

final BufferedReader bufferedReader = new BufferedReader(new 
    InputStreamReader(file.getInputStream(entry)));
String line = "";

while ((line = bufferedReader.readLine()) != null) {
    System.out.println("line" + line);

    final String[] rows = line.split(",");

this is my csv file

" 9:42:43AM","Aug 20, 2015","RaceSummary","Page:1","Id","Race","Type","Rot.","District","PrideFor","ArtSeq","ReportSeq","Content","Type","Md","Bar Group","1","LINC ADAPTER SECTION 4","Content","N","A - ARLIN","1","1","1","Oscar James, Sr.","Content","0","<N.P.>"

i am trying to print the column which i mentioned in the csv.But i dont know why my out put is getting upto "Pride" as one line and "For" as another line like that it was repeating for the next two values ("ArtSeq","ReportSeq").Can any one suggest me where i went wrong. Thanks.

Upvotes: 1

Views: 112

Answers (2)

Sh4d0wsPlyr
Sh4d0wsPlyr

Reputation: 968

Based on the output you provided...

line" 9:42:43AM","Aug 20, 2015","Race Summary","Page: 1","Id","Race","Type","Rot.","District","Pride 
lineFor","Art lineSeq","Report lineSeq","Content","Type","Md","Bar Group","1","LINC ADAPTER SECTION 4","Content","N","A - ARLIN","1","1","1","Oscar James, Sr.","Content","0","<N.P.>" 

Considering it is different then your input, I'd guess there might be a special character or something on the input file (for example, a tab or line spaceing). This is causing your while loop to read the first line (up to the line break), and then read the next line. If you put both of these onto the same line in the file it will probably work better.

I should clarify as well, nothing in the code you posted would cause this behaviour, it is either somewhere else in your code or in the file itself.

Upvotes: 1

StackFlowed
StackFlowed

Reputation: 6816

As you can see in your input you have second value have commas "Aug 20, 2015" this leads to more numbers of splits than that you expect. Example : You would expect this " 9:42:43AM","Aug 20, 2015" to be 2 parts but it will be three

[0]" 9:42:43AM"
[1]"Aug 20
[2] 2015"

You can change you split to be

line.split("\",\""); 

I believe that should solve your problem.

Upvotes: 1

Related Questions