-2

Here is my code: package project4;

import java.util.Scanner;

public class ScoreAnalyzer {

//#OfScores is the length of the scores array
public static int numberOfScores = 0;
public static double[] scores = new double[numberOfScores];
public static double sum = 0.0;
public static double place = 0.0;
//Of sorted list
public static double[] sortedList = new double[numberOfScores];

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of scores you'd like to average: ");
    numberOfScores = input.nextInt();
    scores = new double[numberOfScores];

    for (int count = 0; count < scores.length; count++) {
        System.out.print("Enter score #" + count + ": ");
        place = input.nextDouble();
        scores[count] = place;
        System.out.println("The score of " + scores[count] + " is " + getGrade(count));
    }

    sumOfScores();

    System.out.println("The average of these scores is: " + averageOfScores());

    sort();

    System.out.println("The sorted list of scores (above average scores are "
            + "marked with an\n" + "asterisk '*'): " + sortedList[numberOfScores]);
}

public static char getGrade(int i) {
    //this 
    if (scores[i] >= 90.0) {
        return 'A';
    }
    if (scores[i] >= 80.0) {
        return 'B';
    }
    if (scores[i] >= 70.0) {
        return 'C';
    } 
    if (scores[i] >= 60.0) {
        return 'D';
    } 
    else {
        return 'F';
    }
}

public static double sumOfScores() {
    //sum = 0.0;
    for (int i = 0; i < numberOfScores; i++) {
        sum += scores[i];
    }
    return sum;
}

public static double averageOfScores() {
    double average = sum / numberOfScores;
    return average;
}

public static void sort() {
    for (int i = 0; i < scores.length - 1; i++) {
        // Find the minimum in the list[i..list.length-1]
        double currentMin = scores[i];
        int currentMinIndex;
        currentMinIndex = i;

        for (int j = i + 1; j < scores.length; j++) {
            if (currentMin > scores[j]) {
                currentMin = scores[j];
                currentMinIndex = j;
            }
        }

        // Swap list[i] with list[currentMinIndex] if necessary;
        if (currentMinIndex != i) {
            sortedList[currentMinIndex] = scores[i];
            scores[i] = currentMin;
        }
    }
}

}

The program's purpose is to prompt the user for the number of scores to be entered first, then allow the user to enter that number of scores. As each score is entered, the program should give them a grade for each score inputted. The program then should use a method to find the sum, pass that sum into a method that finds the average, and sort the array in a method.The sorted array should be displayed along with the average and each score which is above the average should be noted with an asterisk beside it.

When I run the program, I am able to enter the number of scores I want to average, input those scores, obtain the sum and average but I'm having problem sorting it to have the asterisk to above average numbers:

Enter the number of scores you'd like to average: 3

Enter score #0: 90

The score of 90.0 is A

Enter score #1: 80

The score of 80.0 is B

Enter score #2: 70

The score of 70.0 is C

The average of these scores is: 80.0

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

at project4.ScoreAnalyzer.sort(ScoreAnalyzer.java:86)

at project4.ScoreAnalyzer.main(ScoreAnalyzer.java:32)
jae
  • 1
  • 2
  • Array indexes in java start at 0 (zero) - not 1 (one). The index of the last element in array `scores` is 2 and not 3. – Abra Oct 26 '19 at 18:51
  • 1
    I see this question has been down-voted and I am curious to know why. The question title adequately describes the problem. The java code is included and constitutes a [mcve]. The background to the problem is explained. The error message is included with the stack trace. What is it about this question that justifies down-voting it? Could it be that perhaps the OP did not search the Internet for `ArrayIndexOutOfBoundsException`? – Abra Oct 26 '19 at 18:59
  • @Abra I doubt this is the minimal example to reproduce the error... at least I can't see how `sumOfScores()` or `sort()` has any influence on the Exception, they are not even been executed. (and missing or wrong (code) formatting is also often reason for downvotes) – user85421 Oct 26 '19 at 19:23
  • Thank you for the clarification, @CarlosHeuberger – Abra Oct 26 '19 at 19:30

1 Answers1

0

There is a problem with the terminating condition of your for loop. Since the array index starts with 0, the highest index will go up to 2 for an array of size 3. However, count, using which you are accessing elements of scores[], is going up to 3 in your for loop and that is the reason for the ArrayOutOfBoundsException. Change the for loop as follows:

for (int count = 0; count < scores.length ; count++)
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    _The problem is with your `for` loop_ Actually the problem is that the code is trying to access element at index 3 of array `scores` and there is no such element - hence the `ArrayIndexOutOfBoundsException` – Abra Oct 26 '19 at 19:03