2014-07-02 76 views
0

我有這個CSV:CSV讀卡器空值Java

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 
      ^----- 

我用csvReader映射到一個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()); 
     } 
    } 

所以問題是,該文件包含之間空字符串,,我在分析得到exeption:java.lang.NumberFormatException:

所致對於輸入字符串: 「」

我解決了。我還有一個問題

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' 

所以,如果一個sentence_id =一個PARENT_ID我要結合這兩個,這樣看起來是這樣的(例如第一線和第二線),但我還應該考慮SORT_ORDER:

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

我不知道如何進行

+1

更改你的bean,使它是一個字符串? –

+1

明顯的例外。你還在期待什麼? –

+0

是的,更改爲一個字符串,然後我會做一個解析,如果它不是null。 – alex1111

回答

1

更改parentIdString BatteryBean中的數據類型。看起來它是整數。