开发者

ERROR HANDLING for my String tokenizer

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";
     开发者_如何学Go           }
                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";
            } 

I want to read a csv file and write it to a database

But here i want to add some error handling .. So how can i do that ..normally my csv file should contain 7 values what if if there are only 4 values

help appreciated ..


You probably could do something like this... create an API to return the value if exist, otherwise throw an exception:-

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

In your code, instead of calling st.nextToken().trim(), you call that 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 ");

    ...
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜