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个值
帮助表示赞赏..
答案 0 :(得分:1)
你可能会做这样的事情......创建一个API来返回值,如果存在,否则抛出异常: -
private String getValue(StringTokenizer st, String name) {
if (st.hasMoreTokens()) {
return st.nextToken().trim();
}
else {
throw new RuntimeException("Missing value for " + name);
}
}
在您的代码中,您调用该API,而不是调用st.nextToken().trim()
: -
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 ");
...
}