ArrayOutOfBoundsException Error?

You have a problem in your Median() method. Try changing

if  (count % 2 == 0){
    median = (scores[scores.length/2-1] + scores[scores.length/2])/2;
}
else {
    median = scores[scores.length/2];
}

to

if (count % 2 == 0)
    median = (scores[count/2] + scores[count/2 - 1])/2;
else
    median = scores[count/2];

Because you have a fixed-size array with 500 elements, the code you have will return the mean value of the items at position 249 and 250, which will be 0 if you have less than 251 values in your array.

Leave a Comment