I have some homework and thought that i got it correct. but when i compile i get the below error. it shows up after i enter my 8 numbers. Also fair warning, i'm not sure why some of the lines aren't indented properly because that isn't how they show up in my code.
Enter a batting average:
.299
Enter a batting average:
.157
Enter a batting average:
.242
Enter a batting average:
.203
Enter a batting average:
.198
Enter a batting average:
.333
Enter a batting average:
.270
Enter a batting average:
.190
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at BattingAverage.main(BattingAverage.java:60)
import java.util.Scanner; public class BattingAverage { public static void main(String args[]) { Scanner s = new Scanner(System.in); // Declare a named constant for array size here. int arraySize = 8; // Declare array here. double[] averages = new double[arraySize]; // Use this integer variable as your loop index. int i; // Use this variable to store the batting average input by user. double x; // String version of batting average input by user. String averageString; // Use these variables to store the minimim and maximum batting averages. double min, max; // Use these variables to store the total and the average. double total, average; // Write a loop to get batting averages from user and assign to array. for(i=0; i < arraySize; i++){ System.out.println("Enter a batting average: "); averageString = s.nextLine(); x = Double.parseDouble(averageString); // Assign value to array. averages[i] = x; } // Assign the first element in the array to be the minimum and the maximum. min = averages[0]; max = averages[0]; // Start out your total with the value of the first element in the array. total = averages[0]; // Write a loop here to access array values starting with averages[1] for(i=1; i < arraySize; i++){ // Within the loop test for minimum and maximum batting averages. if(averages[i] > max){ max = averages[i]; } if(averages[i] < min){ min = averages[i]; } // Also accumulate a total of all batting averages. total = total + averages[i]; } // Calculate the average of the 8 averages. average = total / arraySize; // Print the averages stored in the averages array. System.out.println(averages[i]); // Print the maximum batting average, minimum batting average, and average batting average. System.out.println("Minimum batting average is " +min); System.out.println("Maximum batting average is " +max); System.out.println("Average batting average is " +average); System.exit(0); } }