0

我尝试将字符串数组转换为双精度数组,具体取决于从数据库中选择的气象站 - 有些气象站没有数据,所以整个字符串数组都填充了空值(实际上是 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]);
}
4

1 回答 1

0

如果您在 for 循环之外尝试/捕获,您将停止处理循环的其余部分。只要您不介意数组中留下未分配的值,for 循环中的 Try/catch 可能更接近。像这样的东西可能是最好的,将优化数组更改为 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);
}

在您阅读优化数组的地方添加快速空检查,您就可以了。

于 2013-04-18T00:02:53.560 回答