2014-10-30 88 views
-1

從一個.txt開始計算「年度志願者」的計劃,計算每週的小時數,然後查看他們是否高於平均志願小時數並獎勵積分都。我拋出一個異常,一個數組越界,不能找出爲什麼......在編譯程序時編寫例外程序以進行測試

public class volunteerOfTheYear { 
//declaring int name for indexing because the name will always be in the first 
//postion of the array at nameOfArray[i][0] 
final static int NAME = 0; 

/** 
* @param args the command line arguments 
* @throws java.io.FileNotFoundException 
*/ 
public static void main(String[] args) throws FileNotFoundException{ 
    //declare file handle 
    File inputText = new File("volunteerFile.txt"); 
    //get Scanner from method 
    Scanner input = getInput(inputText); 
    //convert file to 2-d array 
    String[][] volunteerChart = getvolunteerChart(input); 
    //calculate weekly averages and store in array for comprarison 
    double[] weeklyAverages = new double[53]; 
    int week = 1; 
    do{ 
     weeklyAverages[week] = getAverageWeeklyHours(volunteerChart, week); 
     week++; 
    }while(week < 52); 

    //compare volunteer's volunteer hours to weekly averages and calculate points for week 
    //and total volunter's points 
    int[] totalPoints = new int [25]; 
    totalPoints = getVolunteerPoints(volunteerChart, weeklyAverages, week); 
    //display VOFT 


} 
/** 
* 
* @param textFile 
* @return input 
* @throws java.io.FileNotFoundException 
*/ 
public static Scanner getInput(File textFile) throws FileNotFoundException{ 
    Scanner input = new Scanner(textFile); 
    return input; 
} 
/** 
* 
* @param input 
* @return volunteerNamesAndHours 
*/ 
public static String[][] getvolunteerChart(Scanner input){ 
    String[][] volunteerNamesAndHours = new String [25][53]; 
    int ID = 0; 
    int week; 
    do{ 
     volunteerNamesAndHours[ID][NAME] = input.next(); 
      for(week = 1; week < 53; week++){ 
       volunteerNamesAndHours[ID][week] = Integer.toString(input.nextInt()); 
      } 
     ID++;  
    } 
    while(ID <= 24); 

    return volunteerNamesAndHours;   

} 
/** 
* 
* @param volunteerHours 
* @param week 
* @return 
*/ 
public static double getAverageWeeklyHours(String[][] volunteerHours, int week){ 
    double weekTotal = 0; 
    //for(int i = 0; i < 26; i++){ 
    int i = 0; 
    do{ 
     double hours = Double.parseDouble(volunteerHours[i][week]); 
     weekTotal = hours + weekTotal; 
     i++; 
    }while(i < 26); 
    double weeklyAverage = weekTotal/25; 
    return weeklyAverage; 
} 
/** 
* 
* @param volunteerHours 
* @param weeklyAverages 
* @param week 
* @return totalPoints; 
*/ 

public static int[] getVolunteerPoints(String[][] volunteerHours, double[] weeklyAverages, int week){ 
    int [] totalPoints = new int[25]; 
    int personID = 0; 
    do{ 
     for(week = 0; week < 53; week++){ 
      if(weeklyAverages[week+ 1] < Integer.parseInt(volunteerHours[personID][week])) 
       totalPoints[personID] = Integer.parseInt(volunteerHours[personID][week]) + totalPoints[personID] ; 

     } 
     personID++; 
     }while(personID < 25);  
    return totalPoints; 
}  

}在Java中

+0

*,其中*被拋出的異常的時候? – 2014-10-30 19:01:40

+0

異常在線程 「主」 java.lang.ArrayIndexOutOfBoundsException:25 \t在jasonsiboleproject4.volunteerOfTheYear.getAverageWeeklyHours(volunteerOfTheYear.java:93) \t在jasonsiboleproject4.volunteerOfTheYear.main(volunteerOfTheYear.java:39) Java結果:1 – jscincy1 2014-10-30 19:22:17

+0

93號線是什麼? – 2014-10-30 20:19:16

回答

2

陣列(和大多數其他語言)指數從0而不是1,和所以最高的索引比數組大小小1。

所以weeklyAverages [周+ 1](在getVolunteerPoints)是能夠走出去界外周= 52

+0

那麼,我在循環中使用了一個單獨的計數器,還是我只是從一週減去1? – jscincy1 2014-10-30 18:28:31

+1

將周限制爲最多51周(最少爲(周= 0;周<52;周++))或放置一個if函數,在第52周做一些不同的事情。沒有第53周,所以你必須決定你的邏輯交易接着就,隨即。 – FelixMarcus 2014-10-30 18:29:27

+0

同意Felix – ldmtwo 2014-10-30 19:10:59