我的字符串标记生成器的错误处理

发布于 2024-10-18 01:29:04 字数 1971 浏览 2 评论 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 个值怎么办

? ..

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玻璃人 2024-10-25 01:29:04

您可能可以这样做...创建一个 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 ");

    ...
 }

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

    ...
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文