Java 编译程序以进行测试时引发异常
通过计算a.txt中的每周总小时数,然后查看他们是否高于该周的平均志愿者小时数,并为这两项评分,从而计算出年度志愿者。我抛出了一个异常,一个数组超出了边界,我不知道为什么Java 编译程序以进行测试时引发异常,java,arrays,indexoutofboundsexception,throw,Java,Arrays,Indexoutofboundsexception,Throw,通过计算a.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 s
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而不是1索引,因此最高索引比数组大小小一个
因此,当周=52时,getVolunteerPoints中的weeklyAverages[周+1]可以越界。那么,我是在循环中使用一个单独的计数器,还是从周中减去1?将周限制为51,最大值为周=0;周<52周;或者加入一个if函数,在第52周做一些不同的事情。没有第53周,所以你必须决定你的逻辑是如何处理的。异常被抛出到哪里去了?线程主java.lang.ArrayIndexOutOfBoundsException中的异常:25在jasonsiboleproject4.年度志愿者。GetAverage Weekly在jasonsiboleproject4.年度志愿者.年度主要志愿者。java:39 java结果:1和什么第93行是否???weeklyAverages[周]=GetAverageWeeklyHoursvolunterChart,week;不,第93行在方法getAverageWeeklyHours内,异常堆栈可以清楚地看到这一点。