0

I have this CSV:

0,102000082,,2,125,'Battery location','Left-hand drive',2,2
0,300000029,102000082,3,895,'Behind the cab','Left',2,-7
0,102000082,,4,127,'Battery location','Right-hand drive',4,4
            ^-----

I use csvReader to map to a bean

    public static List<BatteryBean> loadAndRead{
    File csvFilename = new File("C:\\my.csv");
    CSVReader csvReader = new CSVReader(new FileReader(csvFilename));

    ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
    strat.setType(BatteryBean.class);
    String[] columns = new String[] { "ktypnr","sentenceId","parentId","sortOrder", "adjItemNoteId","sentText","itemNoteText","parentSortOrder1","parentSortOrder10" };
    strat.setColumnMapping(columns);
    CsvToBean csv = new CsvToBean();
    List<BatteryBean> list = csv.parse(strat, csvReader);
    return list;
}


public static void main(String[] args) {

    try {
        List<BatteryBean> list = loadAndRead("C:\\work\\battery_report_raw.csv");

        for (Object object : list) {
            BatteryBean bb = (BatteryBean) object;
            System.out.println(bb.getKtypnr());
        }
   }

So the problem is that the file contains empty strings between ,, and I get an exeption at parsing :

Caused by: java.lang.NumberFormatException: For input string: ""

I resolved. I have another question

Csv file
ktypnr  sentence_id    parent_id    sort_order  adj_item_note_id      sent_text              iem_note_text
0         102000082                  2                  125             Battery location'   Left-hand drive'
0         300000029    102000082     3                  895             Behind the cab'        Left'
0         102000082                  4                  127             Battery location'       Right-hand drive'
0         300000029    102000082     5                  898             Behind the cab'        Right'

So if one sentence_id = one parent_id i should combine those two so that looks like this(example first line and second line) but I should consider also the sort_order:

0, Battery location, Left-hand drive, Behind the cab, Left

I don't know how to proceed

4
  • 1
    change your bean so that it is a string? Commented Jul 2, 2014 at 8:00
  • 1
    Obvious exception. What else you are expecting ?? Commented Jul 2, 2014 at 8:00
  • Yes, changed to a String, and then I will make a parse if it is not null. Commented Jul 2, 2014 at 8:02
  • Where's BatteryBean? Commented Jul 2, 2014 at 8:04

1 Answer 1

1

Change parentId to String data type in BatteryBean. It seems it is integer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.