Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Java Persistence with Spring Data and Hibernate
this week in the
Spring
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Tim Cooke
Paul Clapham
Liutauras Vilda
Sheriffs:
Junilu Lacar
Rob Spoor
Jeanne Boyarsky
Saloon Keepers:
Stephan van Hulst
Carey Brown
Tim Holloway
Piet Souris
Bartenders:
Forum:
Beginning Java
Cannot throw exception
Dominic Griffin
Greenhorn
Posts: 16
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 1561
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 16
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How do I test whether the numbers are out of bounds
pete stein
Bartender
Posts: 1561
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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.
He does not suffer fools gladly. But this tiny ad does:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Array returning last value entered
Noob Problem
TestScores
Compile error
TestScores
More...