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 に答える