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 文件并将其写入数据库

但是在这里我想添加一些错误处理..那么我该怎么做..通常我的csv文件应该包含7个值如果只有4个值怎么办

帮助表示赞赏..

4

1 回答 1

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 ");

    ...
 }
于 2011-02-15T15:20:56.423 回答