IcedDante
IcedDante

Reputation: 6822

Apache CSVParser is not working

Consider the following code sample:

    private static CSVFormat CSV_FORMAT = CSVFormat.TDF;
    logger.debug("Processing record: {}", line);
    try {
        CSVParser parser = CSVParser.parse(line,CSV_FORMAT);
        if(parser.getRecords().isEmpty()) continue;
        csvRecord = parser.getRecords().get(0);
    } catch (IOException e) {
        logger.warn("Skipping line: " + line,e);
        continue;
    }

For some reason this isn't parsing. I get the following output:

DEBUG:
(OrderParser#parseData:121) - Processing record:
136147091   340834429   4/5/2015 4:35:00
PM  262105109   UFH6285 6   0   0   HWF62 Holmes Humidifier Replacement
Filter  8.99    53.94   0   39.91   0   8.09    0   5.94    5.12        7035997658  Marty   Joe [email protected] Jess
Dude        555 Main st     Anywhere    CA  900000  1

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

and even though you probably cannot see this via StackOverflow I guarantee there are tabs there. However, the index out of bounds exception occurs at parser.getRecords().get(0)

If I make a call to parser.getRecordNumber() it reports 1. I don't know if this is due to a character encoding error or what.

As requested, here's some sample input sandwiched inside of a pre tag. There's two lines here, but I'm just sending the second one in:

SNumber RecD    Receip_ID   ListingID   Date_Entered    234 ReferenceId QT  QT2 QT3 Title   P456    Product_Rev Sh2687t Product Shipping    Comm    ShipFee PerItemFee  Tax_Cost    Company HoneCode    Bill455 Bill788 Email   Ship468644  Ship6489    Ship654132158   Ship98198   Ship_To_City    Ship_To_State   Ship_To_Zip ShippingMethodId
2644    7775    11457   26894   4/9/2015 9:47:00 AM 247520128   700364  1   0   0   Shark Navigator 14.99   14.99   0   11.75   0   2.25    0   0.99    0       84698   Shawn   Vanloan [email protected]  S Vanloo        166 E Main Rd       Anywhere    NY  12000   1

Upvotes: 1

Views: 3528

Answers (2)

IcedDante
IcedDante

Reputation: 6822

Peter Zheng's answer got me started in the right direction. For me the solution came in the code after calling CSVParser.parse. This actually returns an iterable so I proceeded with the following:

CSVParser parser = ... //Any method for obtaining a parser
for(CSVRecord record : parser) {

    //Code for operating on the record here
}

Upvotes: 0

Peter Zberg
Peter Zberg

Reputation: 81

According to the apidocs, parser.getRecords() parses the file according to the given format and returns the content as a list. The parse-position will be at the end of the input. So another call of parser.getRecords() will return an empty list.

Consider parsing the input record wise.

apidocs

Upvotes: 2

Related Questions