2011-02-15 26 views
0
else if (sCarRental.equals("EP")) { 
       sStationCode = st.nextToken().trim(); 
       sName=st.nextToken().trim(); 
       sLocationCode = st.nextToken().trim(); 
       sAddress1=st.nextToken().trim(); 
       sAddress2=st.nextToken().trim(); 
       sPostCode=st.nextToken().trim(); 
       sCity=st.nextToken().trim(); 
       sStationName=sName+sAddress1+sAddress2+sPostCode+sCity; 
       sStationType="C"; 
       if(sLocationCode.equalsIgnoreCase("c")) 
       { 
        sStationArea="C"; 
       } 
       else if (sLocationCode.equalsIgnoreCase("S")) 
       { 
        sStationArea="S"; 
       } 
       else if (sLocationCode.equalsIgnoreCase("N")) 
       { 
        sStationArea="N"; 
       } 
       else if (sLocationCode.equalsIgnoreCase("E")) 
       { 
        sStationArea="E"; 
       } 
       else if (sLocationCode.equalsIgnoreCase("W")) 
       { 
        sStationArea="W"; 
       } 
       else if (sLocationCode.equalsIgnoreCase("T")) 
       { 
        sStationArea="T"; 
       } 
       else if (sLocationCode.equalsIgnoreCase("X")) 
       { 
        sStationArea="R"; 
       } 
       else if (sLocationCode.equalsIgnoreCase("L")) 
       { 
        sStationArea="R"; 
       } 
       else  
       { 
        sStationArea="C"; 
       } 
       sSupplierCode ="EP"; 
       sLocationCode=sStationCode.substring(0, 3); 
       sCrsCode = "EP"; 
      } 

我想讀一個CSV文件,並將其寫入到數據庫錯誤處理我的字符串標記

但在這裏我想添加一些錯誤處理..所以,我怎麼能做到這一點..normally我CSV文件應包含7個值,如果如果只有4個值

幫助表示讚賞..

+0

什麼是 「ST」 嗎? – Neutralizer 2011-02-15 15:11:19

+0

@Umair StringTokenizer我認爲。 http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html – 2011-02-15 15:13:58

回答

1

你也許可以做這樣的事情......創造了一個API來返回,如果存在價值,否則拋出一個異常: -

private String getValue(StringTokenizer st, String name) { 
    if (st.hasMoreTokens()) { 
     return st.nextToken().trim(); 
    } 
    else { 
     throw new RuntimeException("Missing value for " + name); 
    } 
} 

在你的代碼,而不是調用st.nextToken().trim(),您調用API: -

else if (sCarRental.equals("EP")) { 

    sStationCode = getValue(st, "station code"); 
    sName=getValue(st, "name"); 
    sLocationCode = getValue(st, "location code"); 
    sAddress1=getValue(st, "address 1"); 
    sAddress2=getValue(st, "address 2"); 
    sPostCode=getValue(st, "post code"); 
    sCity=getValue(st, "city "); 

    ... 
}