從一個.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中
*,其中*被拋出的異常的時候? – 2014-10-30 19:01:40
異常在線程 「主」 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
93號線是什麼? – 2014-10-30 20:19:16