在编译程序时抛出异常以进行测试

时间:2014-10-30 18:03:33

标签: java arrays indexoutofboundsexception throw

制定计划“年度志愿者”的计划,从.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;
}       

}

1 个答案:

答案 0 :(得分:2)

Java(和大多数其他语言)中的数组从0开始索引而不是1,因此最高索引比数组大小少一个。

所以weekAverages [week + 1](在getVolunteerPoints中)在周= 52时能够超出范围。