I need to create a program that calculates the average of the test scores the user entered. This is actually the second time i've had an assignment asking to make this program. But this time the assignment wants me to throw an IllgalArgumentException whenever the user enters a testscore less than 0 or greater than 100. The problem is that no matter what test score even when the test score is between 0-100 the program crashes with an IllegalArgumentException. Not sure what i'm doing wrong any ideas here the expected output without the exception
Expected output
Enter number of test scores:5
Enter test score 1:70
Enter test score 2:65
Enter test score 3:94
Enter test score 4:55
Enter test score 5:90
74.8
and my code
public static void main(String[] args) throws IllegalArgumentException
{
Scanner input = new Scanner (System.in);
int numTests = 0;
int [] grade = new int [numTests];
double average;
double totalGrades = 0;
System.out.print("Enter number of test scores:");
numTests = input.nextInt();
grade = new int[numTests];
for (int index = 0; index < grade.length; index++)
{
System.out.print("Enter test score " + (index + 1) + ":");
grade[index] = input.nextInt();
if(grade[index] < 0 || grade[index] > 100);
{
throw new IllegalArgumentException();
}
}
for (int i = 0; i < grade.length; i++)
{
totalGrades += grade[i];
}
average = totalGrades / grade.length;
System.out.println(average);
}
private static class IllegalArgumentException extends Exception
{
public IllegalArgumentException()
{
super("Test scores must have a value less than 100 and greater than 0.");
}
public IllegalArgumentException(int value)
{
super("Invalid Test Score: " + value);
}
}