Decompose the problem, please.
Start with the statistics:
/**
* Statistics
* @author Michael
* @link http://stackoverflow.com/questions/15626262/averaging-grades-using-java-swing?noredirect=1#comment22167503_15626262
* @since 3/25/13 7:50 PM
*/
public class Statistics {
public static double getAverage(int numValues, int [] values) {
double average = 0.0;
if ((values != null) && (numValues > 0) && (values.length >= numValues)) {
for (int i = 0; i < numValues; ++i) {
average += values[i];
}
average /= numValues;
}
return average;
}
}
Next I'd recommend that you leave Swing out of it altogether for a little while. Do a text-only input/output UI.
import java.util.Scanner;
/**
* StatisticsDriver
* @author Michael
* @link http://stackoverflow.com/questions/15626262/averaging-grades-using-java-swing?noredirect=1#comment22167503_15626262
* @since 3/25/13 7:50 PM
*/
public class StatisticsDriver {
public static final int MAX_VALUES = 50;
public static void main(String [] args) {
int [] values = new int[MAX_VALUES];
Scanner scanner = new Scanner(System.in);
boolean getAnotherValue;
int numValues = 0;
do {
System.out.print("next value: ");
String input = scanner.nextLine();
if (input != null) {
values[numValues++] = Integer.valueOf(input.trim());
}
System.out.print("another? [y/n]: ");
input = scanner.nextLine();
getAnotherValue = "y".equalsIgnoreCase(input);
} while (getAnotherValue);
System.out.println(Statistics.getAverage(numValues, values));
}
}
Now that you have those, turn your attention to Swing.
Too many young programmers get themselves wound around the axle on Swing before they solve the problem. Don't make that mistake.