0

I have a csv encoding a thesaurus of some items and, expectedly, the number of entries per row are different for different rows.

The first line contains 25 tokens/synonyms. The rest of the lines have lesser. But the String[] that are read all have length 25. The shorter rows are padded with empty strings.

Is there any way to avoid this from happening?

My code looks like this:

CSVReader reader = new CSVReader(new FileReader("thesaurus.csv", '\t'));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    System.out.println("length of the row: "+ nextLine.length);
}

Sample lines from csv:

search  examination  exploration    hunt    inquiry inspection  investigation   pursuit quest   research    chase   frisking    going-over  inquest pursual pursuance   pursuing    rummage scrutiny    shakedown   fishing expedition  legwork perquisition    wild-goose chase    witch hunt
school  schule
saint   st. st

When I print the String[] items one-by-one, I get this:

'school', 'schule', , , , , , , , , , , , , , , , , , , , , , , , 
'saint', 'st.', 'st', , , , , , , , , , , , , , , , , , , , , , , 
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Navneet
  • 9,590
  • 11
  • 34
  • 51
  • @Reimeus Added. I didn't mean explicit padding. What I meant was that the way the String[] is initialized in `reader.readNext()` might be the problem. Or I am missing something really obvious... Thanks. – Navneet Aug 22 '13 at 22:24
  • @Reimeus I don't have trailing tabs in my csv. I post-processed to `removeAll()` the empty or null entries. – Navneet Aug 23 '13 at 17:59

1 Answers1

0

I guess you have trailing spaces in your input csv.

String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        for (String line : nextLine) {
            System.out.println(line.replaceAll(" ", "."));
        }
    }

This is the output if there are no trailing spaces.

search..examination..exploration....hunt....inquiry.inspection..investigation...pursuit.quest...research....chase...frisking....going-over..inquest.pursual.pursuance...pursuing....rummage.scrutiny....shakedown...fishing.expedition..legwork.perquisition....wild-goose.chase....witch.hunt
school..schule
saint...st..st

This is the output if there are trailing spaces.

search..examination..exploration....hunt....inquiry.inspection..investigation...pursuit.quest...research....chase...frisking....going-over..inquest.pursual.pursuance...pursuing....rummage.scrutiny....shakedown...fishing.expedition..legwork.perquisition....wild-goose.chase....witch.hunt
school..schule............................
saint...st..st.............................................
baskar_p
  • 665
  • 5
  • 16