I am working on this program and can't get my InvalidTestScore exception to work. Please help!

public class Grades extends Exception
{	// Begin public class Grades
	private double[] testScores;

		@param scoreArray An array of test scores

	public Grades(double[] scoreArray)
	// Assign the array argument to the testScores field
	testScores = scoreArray;

		getAverage method
		@return The average of the test scores

	public double getAverage()
	{	// Begin public double getAverage()

	double total = 0;	// To hold the score total
	double average;		// To hold the average

		// If array contains less than 4 test score, display error message and set
		// average to 0
		if (testScores.length < 4)
		{	// Begin if statement
			System.out.println("Error: You must have atleast " +
				"two test scores!");

			average = 0;
		}	// End if statement
		{	// Begin else statement
			// Calculate total of the scores
			for (double score : testScores)

			total += score;			// Get the average of the scores
			average = total / (testScores.length);
		}	// End else statement

		// Return the average of the test scores
		return average;

	}	// End public double getAverage()

	private String[] studNames;		// Variable that will ref an array of student names

			@param namesArray An array of student names

	}	// End public class Grades
import java.text.DecimalFormat;		// Needed to format test score average
import javax.swing.JOptionPane;		// Needed for showInputDialog & showMessageDialog

public class TestScore
{	// Begin public class TestScore

	public static void main(String[] args)
	{	// Begin public static void main(String[] args)

		int numScores = 4;					// To hold number of score
		double average = 0;					// Average of test scores
		String input;						// To hold input

		// Get number of test scores
		input = JOptionPane.showInputDialog("Please enter number of test scores. ");

		// Create an array to hold the test scores
		double[] scores = new double[numScores];

		// Get the test scores and store them in the scores array
		for (int index = 0; index < numScores; index++)
		{	// Begin for loop
			input = JOptionPane.showInputDialog("Enter score" +
								    (index + 1) + ": ");

			if (Integer.parseInt(input) < 0 || Integer.parseInt(input) > 100)
			{	// Begin if statement
				throw new InvalidTestScore(
						"Test scores must be greater than 0 \n" +
							"less than 100!");
			}	// End if statement

			scores[index] = Double.parseDouble(input);

		}	// End for loop

		// Create a Grades object, passing the scores array as an argument to the constructor
		Grades myGrades = new Grades(scores);

		// Display the average of the test scores
							"Your test score average is \n" +

		// Exit system

	}	// End public static void main(String[] args)

}	// End public class TestScore

In this scenario, you are trying to make sure an Integer is in a certain range. So use a while loop and continuously prompt the user for a value until they enter one that is in the correct range. Generally, when you anticipate an event (such as an Integer that isn't in the right range) and can deal with it without Exceptions, you should not use Exceptions. An example of why you would need to use Exceptions here is if the user enters something that is not parseable as an Integer.. in which case, the parseInt method would throw an exception - probably InputMismatchException - (since it cannot reasonably deal with the condition where the String you gave it isn't an Integer) and your calling method would have to deal with that possibility.

A more reasonable scenario for your InvalidTestScore exception might be, lets say you have an Excel file where you have a list of test scores. Now lets say you use a Java program to read in those test scores from the Excel file. You could create a method addTestScore(int score) and use it to add each test score. If the user input an invalid score in their Excel file (< 0 or > 100) then you'd throw new InvalidTestScore, that way you can let the calling method deal with the scenario where that happened.

Anyway, I'm not trying to be critical or anything, but the code you gave just doesn't make sense to me. Who is going to catch the Exception you threw? My guess is nobody can catch it and the JVM will have to deal with it, more than likely leading to some behavior you don't want. Check out this link:


This article has been dead for over six months. Start a new discussion instead.