Working on program that calculates the "Volunteer of the Year" by totaling weekly hours from a .txt and then seeing if they are above average volunteer hours that week and awarding points for both. I am throwing an exception with an array being out of bounds and can't figure out why...
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;
}
}
Arrays in Java (and most other languages) index from 0 not 1, and so the highest index is one less than the array size.
So weeklyAverages[week + 1] (in getVolunteerPoints) is able to go out of bounds when week = 52.