java - Throwing a exception while compiling the program to test as going -
java - Throwing a exception while compiling the program to test as going -
working on programme calculates "volunteer of year" totaling weekly hours .txt , seeing if above average volunteer hours week , awarding points both. throwing exception array beingness out of bounds , can't figure out why...
public class volunteeroftheyear { //declaring int name indexing because name in first //postion of array @ nameofarray[i][0] final static int name = 0; /** * @param args 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 method scanner input = getinput(inputtext); //convert file 2-d array string[][] volunteerchart = getvolunteerchart(input); //calculate weekly averages , store in array comprarison double[] weeklyaverages = new double[53]; int week = 1; do{ weeklyaverages[week] = getaverageweeklyhours(volunteerchart, week); week++; }while(week < 52); //compare volunteer's volunteer hours weekly averages , calculate points 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); homecoming 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); homecoming volunteernamesandhours; } /** * * @param volunteerhours * @param week * @return */ public static double getaverageweeklyhours(string[][] volunteerhours, int week){ double weektotal = 0; //for(int = 0; < 26; i++ ){ int = 0; do{ double hours = double.parsedouble(volunteerhours[i][week]); weektotal = hours + weektotal; i++; }while(i < 26); double weeklyaverage = weektotal / 25; homecoming 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); homecoming totalpoints; }
}
arrays in java (and other languages) index 0 not 1, , highest index 1 less array size.
so weeklyaverages[week + 1] (in getvolunteerpoints) able go out of bounds when week = 52.
java arrays indexoutofboundsexception throw
Comments
Post a Comment