A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Cannot throw exception
Dominic Griffin
Greenhorn
Joined: Jun 10, 2009
Posts: 16
posted
Jul 12, 2009 18:52:39
0
I am trying to throw an IllegalArgumentException that will catch a negative number and a number over 100 when entered as a test score. When I run the program it still allows negative numbers to b e entered and numbers over 100. I have a TestScore class and InvalidTestScore Class. import java.util.Scanner; // For Scanner class public class TestScores { private int[] scores; // References the score data public TestScores(int[] s) throws IllegalArgumentException // Constructor accepts array as argument { if (s.length < 0) throw new IllegalArgumentException("Score is a negative number."); if (s.length > 100) throw new IllegalArgumentException("Score is greater than 100."); scores = new int[s.length]; for (int index = 0; index < s.length; index ++) scores[index] = s[index]; } public int getTotal() { int total = 0; // Accumulator for (int value : scores) total += value; return total; // Return the total } public int getAverage() { return getTotal()/ scores.length; } public static void main(String[] args) { final int STUDENT_SCORES = 6; // Number of array elements int[] scores = new int[STUDENT_SCORES]; // Array to hold scores getValues(scores); TestScores points = new TestScores(scores); System.out.println(); System.out.println("The average test score for six students are " + (points.getAverage())); } private static void getValues(int[] array) { Scanner keyboard = new Scanner(System.in); // New Scanner object System.out.println("Enter a whole numeric score for each student."); for (int index = 0; index < array.length; index++) // For Loop { System.out.print("Student " + (index + 1) + ": "); array[index] = keyboard.nextInt(); } } }
public class InvalidTestScore extends Exception { public InvalidTestScore() { super("Error: Negative score entered"); } public InvalidTestScore(int[] s) { super("Error: Negative score entered: " + s.length); } }
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
posted
Jul 12, 2009 19:07:20
0
You seem to be
testing
if the array's length is < 0 (impossible) or > 100, not whether the numbers it contains are out of bounds.
Dominic Griffin
Greenhorn
Joined: Jun 10, 2009
Posts: 16
posted
Jul 12, 2009 19:14:17
0
How do I
test
whether the numbers are out of bounds
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
posted
Jul 12, 2009 20:01:22
0
How do I test whether the numbers are out of bounds
By iterating through the array with a for-loop (as you already are doing), and testing each array element to see if it is in bounds or not.
I agree. Here's the link:
http://aspose.com/file-tools
subject: Cannot throw exception
Similar Threads
TestScores
Noob Problem
Array returning last value entered
TestScores
Compile error
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter