2013-04-17 59 views
0

根據從數據庫中選擇的氣象站,我嘗試將字符串數組轉換爲雙精度數組 - 一些氣象站沒有數據,因此整個字符串數組是充滿了空值(實際上其中12個)因此,如果選擇其中一個電臺,我會得到一個例外。我真的沒有時間寫很多代碼來解決它,因爲我必須很快提交我的工作......是否有任何解決方案來捕捉它並向用戶顯示一條錯誤消息insted的行和行解釋錯誤?謝謝!在將字符串轉換爲雙精度時檢查java中的錯誤

這裏是循環的字符串數組轉換爲雙陣列

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]); 
} 
+0

我不明白,你想做什麼,當你遇到一個null,你想打印一個錯誤?或者你想保存0嗎?一個-1? – jsedano

+0

是的,我想打印en錯誤,我認爲它甚至不是0 - 如果它是我不會得到一個錯誤,它是NULL –

+0

而NULL不能轉換爲雙重 –

回答

0

如果你的try/catch之外循環,你會停止處理循環的其餘部分。 try/catch裏面的for循環可能更接近,只要你不介意在數組中留下未賦值的值。像這樣的東西可能是最好的,不斷變化的優化陣列式雙[]:

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

添加在你閱讀優化陣列,你是金色的快速null檢查。

相關問題