在将字符串转换为double时检入java时出错

时间:2013-04-17 22:02:57

标签: java jsp exception error-handling

我试图将字符串数组转换为双数组,具体取决于从数据库中选择的气象站 - 某些气象站没有数据,所以整个字符串数组都填充了空值(事实上其中12个)所以很明显,如果选择其中一个电台,我会得到一个例外。我真的没有时间编写很多代码来解决它,因为我必须很快提交我的工作...是否有任何解决方案来捕获它并向用户显示一条错误消息,用于解释错误的行和行?谢谢!

这是将字符串数组转换为双数组的循环

for(int i = 0; i<12; i++)
{   
avMaxTempOptimised[i] = Double.parseDouble(avMaxTempSplit[i]);
avMinTempOptimised[i] = Double.parseDouble(avMinTempSplit[i]);
meanTempOptimised[i] = Double.parseDouble(meanTempSplit[i]);
highestTempOptimised[i] = Double.parseDouble(highestTempSplit[i]);
lowestTempOptimised[i] = Double.parseDouble(lowestTempSplit[i]);
maxWindOptimised[i] = Double.parseDouble(maxWindSplit[i]);
totalRainfallOptimised[i] = Double.parseDouble(totalRainfallSplit[i]);
maxDayRainfallOptimised[i] = Double.parseDouble(maxDayRainfallSplit[i]);
rainDaysOptimised[i] = Double.parseDouble(rainDaysSplit[i]);
totalSunshineOptimised[i] = Double.parseDouble(totalSunshineSplit[i]);
mostSunshineDayOptimised[i] = Double.parseDouble(mostSunshineDaySplit[i]);

avMaxTemp2Optimised[i] = Double.parseDouble(avMaxTemp2Split[i]);
avMinTemp2Optimised[i] = Double.parseDouble(avMinTemp2Split[i]);
meanTemp2Optimised[i] = Double.parseDouble(meanTemp2Split[i]);
highestTemp2Optimised[i] = Double.parseDouble(highestTemp2Split[i]);
lowestTemp2Optimised[i] = Double.parseDouble(lowestTemp2Split[i]);
maxWind2Optimised[i] = Double.parseDouble(maxWind2Split[i]);
totalRainfall2Optimised[i] = Double.parseDouble(totalRainfall2Split[i]);
maxDayRainfall2Optimised[i] = Double.parseDouble(maxDayRainfall2Split[i]);
rainDays2Optimised[i] = Double.parseDouble(rainDays2Split[i]);
totalSunshine2Optimised[i] = Double.parseDouble(totalSunshine2Split[i]);
mostSunshineDay2Optimised[i] = Double.parseDouble(mostSunshineDay2Split[i]);
}

1 个答案:

答案 0 :(得分:0)

如果你尝试/捕捉外部循环,你将停止处理循环的其余部分。 try / catch inside for循环可能更接近,只要你不介意数组中剩下的未赋值。这样的事情可能是最好的,将Optimized数组更改为Double []:

for(int i = 0; i<12; i++)
{   
avMaxTempOptimised[i] = safeDouble(avMaxTempSplit[i]);
avMinTempOptimised[i] = safeDouble(avMinTempSplit[i]);
meanTempOptimised[i] = safeDouble(meanTempSplit[i]);
highestTempOptimised[i] = safeDouble(highestTempSplit[i]);
lowestTempOptimised[i] = safeDouble(lowestTempSplit[i]);
maxWindOptimised[i] = safeDouble(maxWindSplit[i]);
totalRainfallOptimised[i] = safeDouble(totalRainfallSplit[i]);
maxDayRainfallOptimised[i] = safeDouble(maxDayRainfallSplit[i]);
rainDaysOptimised[i] = safeDouble(rainDaysSplit[i]);
totalSunshineOptimised[i] = safeDouble(totalSunshineSplit[i]);
mostSunshineDayOptimised[i] = safeDouble(mostSunshineDaySplit[i]);

avMaxTemp2Optimised[i] = safeDouble(avMaxTemp2Split[i]);
avMinTemp2Optimised[i] = safeDouble(avMinTemp2Split[i]);
meanTemp2Optimised[i] = safeDouble(meanTemp2Split[i]);
highestTemp2Optimised[i] = safeDouble(highestTemp2Split[i]);
lowestTemp2Optimised[i] = safeDouble(lowestTemp2Split[i]);
maxWind2Optimised[i] = safeDouble(maxWind2Split[i]);
totalRainfall2Optimised[i] = safeDouble(totalRainfall2Split[i]);
maxDayRainfall2Optimised[i] = safeDouble(maxDayRainfall2Split[i]);
rainDays2Optimised[i] = safeDouble(rainDays2Split[i]);
totalSunshine2Optimised[i] = safeDouble(totalSunshine2Split[i]);
mostSunshineDay2Optimised[i] = safeDouble(mostSunshineDay2Split[i]);
}


...

safeDouble( String str){
return str== null ? null : Double.parseDouble(str);
}

添加快速空值检查,您可以在其中读取优化数组,并且您是黄金。