2016-04-21 103 views
0

我收到一個「在線程中的異常」main「java.lang.ArrayIndexOutOfBoundsException:5在WeatherAnalysis.main(WeatherAnalysis.java:37)」我不知道線路上出現了什麼問題37,日期[CurrentLine] [0] =(int)fullDate%100 ;.嘗試使用.txt文件輸出的天氣模式從1941年至2013年線程「主」線中的異常37

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class WeatherAnalysis { 

    public static void main(String[] args) throws FileNotFoundException { 

     int NumberOfDays = 0; 
     String station; 
     File f = new File("PortlandWeatherCut.txt"); //Import text file. 
     Scanner input= new Scanner(f); 
     input.nextLine(); 
     input.nextLine(); 

     while (input.hasNextLine()) { //Assign number of days to be   
      NumberOfDays++; 
      input.nextLine(); 
     } 

     double[][] date = new double[NumberOfDays][3]; 
     double[] prcp = new double[NumberOfDays]; 
     double[] snow = new double[NumberOfDays]; 
     double[] snwd = new double[NumberOfDays]; 
     double[] tmax = new double[NumberOfDays]; 
     double[] tmin = new double[NumberOfDays]; 
     int CurrentLine = 0; 
     double fullDate = 0.0; 

     File g = new File("PortlandWeather.txt"); //Import text file. 
     Scanner weather= new Scanner(g); 
     weather.nextLine(); weather.nextLine(); 

     while(weather.hasNextLine()) { //variables and converts to inches. 
     station = weather.next(); 
     fullDate = weather.nextDouble(); 
     date[CurrentLine][0] = (int)fullDate % 100; 
     date[CurrentLine][1] = ((int)fullDate % 10000)/100; 
     date[CurrentLine][2] = (int)fullDate/10000; 
     prcp[CurrentLine] = weather.nextDouble()* .00393701; 
     snow[CurrentLine] = weather.nextDouble()* 0.0393701; 
     snwd[CurrentLine] = weather.nextDouble()* 0.0393701; 
     tmax[CurrentLine] = (weather.nextDouble()/10.0) * (9.0/5.0) + 32.0; 
     tmin[CurrentLine] = (weather.nextDouble()/10.0) * (9.0/5.0) + 32.0; 
     CurrentLine++; 
     weather.nextLine(); 
    } 
    int indexPoint = 0; 
    int nextPoint = 1; 
    int endPoint = 0; 
    for (int x = 1; x < date.length; x++){ 
     //loop that calculates array section to be averaged, 
     //uses a method to average them, then prints a table 
     do { 
       nextPoint++; 
       endPoint++; 
     } 
     while(((int)date[nextPoint - 1][02]/10) == ((int)date[nextPoint][1]/10)); 
     if (nextPoint < date.length) { 
     System.out.printf("%4.0f's Average Max Temp = %4.1f\tAverage Min Temp = %4.1f\n", 
       date[indexPoint][02],arrayAvg(tmax, indexPoint, endPoint), 
        arrayAvg(tmin, indexPoint, endPoint)); 
      indexPoint = nextPoint; 
      } else 
       System.out.println(); 

     } 
    } 
    public static double arrayAvg(double a[], int fromIndex, int toIndex) { 
     //Averages the values between the entered parameters and returns it. 
     double sum = 0; 
     double divisor = 0; 
     for (int i = fromIndex; i < toIndex; i++){ 
      if (a[i] == 393.6616299) { 
       divisor++; 
      } else { 
       sum += a[i]; 
       divisor++; 
      } 
     } 
     double average = sum/divisor; 
     return average; 
    } 
} 
+0

'PortlandWeather.txt'比'PortlandWeatherCut.txt'多了幾行,也許? –

+0

確實有更多的行 –

+0

PortlandWeatherCut.txt的內容是什麼? –

回答

0

好像你的問題是在線路37我認爲這是

date[currentline][0] = (int)fullDate%100 

內部while循環。循環繼續在創建日期2維數組的NumberOFdAYS之上,因此它會拋出回合。

我的第一個建議是將這個怪物功能分解爲更小的功能(查找單一責任原則)。

我的第二個建議是做TDD(測試驅動開發)

但是,如果你想快速和骯髒的調試,做在你的while循環的開頭如下:

System.out.printf("Current line = %d, does weather has next line? = %b", CurrentLine, weather.hasNextLine) 

這將在例外之前顯示currentLine的所有值,並顯示布爾值的值。

相關問題