Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA - OOP my data fields from my file are years and this is to fill an array to

ID: 3861824 • Letter: J

Question

JAVA - OOP

my data fields from my file are years and this is to fill an array to show the min/max/avg. I had it working when I was using a decimal for the field, but I'm switching to the year. I keep getting this error, and while I know where it is telling me the error is, I don't know how to fix it

174 //fill array 175 public static int fillArray (trackData tracks int nTracks, int fields 176 double max, double min) double range max-min: int width (int) (range/ 10. 0) +1: 178 179 double counterIndex: 180 for (int i 1; iknTracks: ++i) 181 counter Index tracks [i]. length-min; 182 int integerIndex (int) counterIndex: 183 184 fields [integer Index 185 186 return width: 187

Explanation / Answer

The problem here is, your array fields[] at line 184 is going out of index. Check with the size of the array you declared for fields[], and what is the range of values for integerIndex.... This value is being used as an index for the array at line 184. If that value goes beyond the array limit, this problem arises.

For example, if the size of the array fields[] is 10, and at some point if the integerIndex variable goes to a value beyond the range 0 - 9, i.e., a value that is either greater than 9, or is less than 0, then this problem arises.

If you want to simply avoid this problem, you can use a condition here at line 184:

if(integerIndex >= 0 && integerIndex < fields.length)

fields[integerIndex]++;

This will make sure that your values are not going beyond the index of the array, which will avoid getting this ArrayIndexOutOfBoundsException. But be mindful, this could lead to loosing some values from being updated.