0

I'm new to coding and I need a little help with this assignment. I keep getting the "ArrayIndexOutOfBoundsException" no matter what I try. I can get the program to work until I enter the last batting average, and then I receive the error.

expected to enter in 8 batting averages like .250, .302, etc

This is the exact error message. "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at BattingAverage.main(BattingAverage.java:38)"

Thank you for any help in advance!

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.
        final int MAX_AVERAGES = 8;

        // Declare array here.
        double averages[] = new double[MAX_AVERAGES];

        // Use this integer variable as your loop index.
        int loopIndex; 

        // Use this variable to store the batting average input by user.
        double battingAverage = 0;

        // 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(loopIndex = 0; loopIndex < MAX_AVERAGES; loopIndex++)
          {
           System.out.println("Enter a batting average: ");
        
           averageString  = s.nextLine();
           battingAverage = Double.parseDouble(averageString);

          }
           // Assign value to array.
           averages[loopIndex] = battingAverage;
        
        // 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(loopIndex = 1; loopIndex<MAX_AVERAGES; loopIndex++)
        {
           // Within the loop test for minimum and maximum batting averages.
           if(averages[loopIndex]<min)
                min = averages[loopIndex];
           if(averages[loopIndex]>max)
                max = averages[loopIndex];
           // Also accumulate a total of all batting averages.
           total += averages[loopIndex];
        }
           
        // Calculate the average of the 8 averages.
        average = total/ MAX_AVERAGES;

        // Print the averages stored in the averages array. 
        for(loopIndex = 0; loopIndex<MAX_AVERAGES; loopIndex++)
        {
            System.out.println("averages" + averages[loopIndex]);
        }
        
        // 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);

    }
}
aleena807
  • 13
  • 1

1 Answers1

0

Here

// Write a loop to get batting averages from user and assign to array.
for(loopIndex = 0; loopIndex < MAX_AVERAGES; loopIndex++)
{
     System.out.println("Enter a batting average: ");
   
     averageString  = s.nextLine();
     battingAverage = Double.parseDouble(averageString);

}

// Assign value to array.
averages[loopIndex] = battingAverage;

At the end of the loop loopIndex is incremented before checking for the exit condition loopIndex < MAX_AVERAGES, so after the last cycle loopIndex is equal to MAX_AVERAGES.

Trying to access that index of the averages array (since the last valid index is MAX_AVERAGES - 1) throws an exception.

Federico klez Culloca
  • 22,898
  • 15
  • 55
  • 90