Results 1 to 6 of 6
- 04-08-2011, 04:36 PM #1
Member
- Join Date
- Apr 2011
- Location
- Florida
- Posts
- 6
- Rep Power
- 0
Confused in Exception Handling Assignment!!
I have come to a logical impasse with my current exception handling assignment! :confused:
The purpose of the assignment is to use try and catch blocks to implement exception handling logic.
The assignment requirements follow:
1. Create your exception in a file called ScoreException.java. Create the UseScoreException.java class to use this exception. The UseScoreException.java file will have the main() method.
2. Prompt the user to enter a test score. Read this score using a dialog box.
3. Any input value greater than 100 or less than 0 should generate the ScoreException exception. You must throw a ScoreException exception at least once and you must have a catch block that catches a ScoreException exception. Use the getMessage method of the Throwable class to display the message associated with this exception. You may do other processing also if you wish. This catch block must display a dialog box with "The score must be >= 0 and <= 100!".
4. Display a dialog box with "Do you want to enter another score?" after the user clicks the OK button.
5. Use the "parseXXXX" methods to convert the String read from the input dialog. This method will throw a NumberFormatException if it is unable to convert the String object to a valid number. Any input value that has characters other than numbers, a decimal, or a negative sign will generate the NumberFormatException exception. You must have a catch block that catches a NumberFormatException exception. This catch block must display a dialog box with "You must enter a number for the score!".
6. Display a dialog box like #4 above ("Do you want to enter another score?") after the user clicks the OK button.
7. Display a dialog box with "That is a valid score." if a valid score is entered.
8. Display a dialog box like #4 above ("Do you want to enter another score?") after the user clicks the OK button.
9. Continue the steps above until the user clicks the No command button when presented with the dialog box in #4 ("Do you want to enter another score?").
I don't need to worry about the program logic for the Cancel command or the Cancel icon.
Here is the code for my ScoreException class:
Java Code:public class ScoreException extends Exception {//begin class public ScoreException()//constructs new Exception, takes no arguments { super("The score must be >= 0 and <= 100!");//Specified detail message } }//end class
Here is the code for my UseScoreException (sorry for all the comments, it helps to keep me organized!)
Java Code:import javax.swing.*; class UseScoreException {//begin class public static void main(String[] args) throws ScoreException {//begin main method //declaration of class variables int score = 0;//declares and initializes variable String inputString;//declares String that will accept user input final int MAX_SCORE = 100;//declares max limit for scores boolean isYes; try {//begin try block do {//begin do block of statements inputString = JOptionPane.showInputDialog(null, "Enter test score for student.", "Enter Score", JOptionPane.QUESTION_MESSAGE); score = Integer.parseInt(inputString);//holds input value String and converts it to int if (score > MAX_SCORE || score < 0)//evaluates score input against upper and lower limits {//begin "if" block of statements throw new ScoreException(); }//end "if" block of statements else {//begin "else" block of statements JOptionPane.showMessageDialog(null, "That is a valid score.", "Valid Score", JOptionPane.INFORMATION_MESSAGE); }//end "else" block of statements }//end do block of statements while (score > MAX_SCORE || score < 0); }//end try block catch(ScoreException se) {//begin ScoreException block of statements JOptionPane.showMessageDialog(null, se.getMessage(), "Score Error", JOptionPane.ERROR_MESSAGE); }//end ScoreException block of statements /*try {//begin try block int selection = JOptionPane.showConfirmDialog(null, "Do you want to enter another score?", "Enter Score", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); do { inputString = JOptionPane.showInputDialog(null, "Enter test score for student.", "Enter Score", JOptionPane.QUESTION_MESSAGE); score = Integer.parseInt(inputString);//holds input value String and converts it to int } while (score <= MAX_SCORE || score >= 0); //(isYes = (selection == JOptionPane.YES_OPTION)); }//end try block */ catch(NumberFormatException nfe) {//begin NumberFormatException block of statements JOptionPane.showMessageDialog(null, "You must enter a number for the score!", "Score Error", JOptionPane.ERROR_MESSAGE); }//end NumberFormatException block of statements }//end main method }//end class
I know I need to write the code (second try block after the catch ScoreException()) that I have within the multiple line comment block into the first try block, but I can't figure out how to do it correctly. That's why it's sitting as a comment for now. The logic on how to write this correctly without using an endless series of if-else statements escapes me! I've been grinding away at this part for over two days straight.
Can anyone help me please?
- 04-08-2011, 04:47 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 34
- Rep Power
- 0
Here's some pseudo code, try something along these lines...
Java Code:score = 0 while score != 0 try{ inputString = JOptionPane.showInputDialog(null, "Enter test score for student.", "Enter Score", JOptionPane.QUESTION_MESSAGE); int inputScore = Integer.parseInt(inputString); if (score > MAX_SCORE || score < 0) throw new ScoreException(); else score = inputScore }catch(ScoreException e){ JOptionPane.showMessageDialog(null, "That is a valid score.", "Valid Score", JOptionPane.INFORMATION_MESSAGE); //if the user cancels, code a break here; } }
-
Get rid of most of those comments when posting code here because it really makes your code hard to read. I think that you probably don't want this line in your ScoreException constructor:
Java Code:super("The score must be >= 0 and <= 100!");//Specified detail message
as it adds little. Rather perhaps you want two constructors, one the default constructor and another that takes a String parameter and passes that String to the super constructor.
In your code that uses the exception, perhaps you want to create a method that throws the exception if the input is bad, and then in another method call the first method in a try/catch block that catches this exception.
e.g.,
Java Code:public class Foo003 { public static void main(String[] args) { //... some code here try { methodThrowsException(); } catch (ScoreException e) { // TODO: handle exception } } public static void methodThrowsException() throws ScoreException { // get test score, // if bad, throw the exception } }Last edited by Fubarable; 04-08-2011 at 04:51 PM.
-
It seems funny (at least to me) to throw the exception from within that try block. Why not simply us if/else rather than an exception that can't possibly percolate up a stack chain? Maybe it's just me, but it seems much more natural to have a method throw the exception as in my example.
- 04-10-2011, 12:44 PM #5
Member
- Join Date
- Apr 2011
- Location
- Florida
- Posts
- 6
- Rep Power
- 0
Thank you both for the help!
Using a while loop with the boolean condition was what worked for me.Last edited by rugger06; 04-10-2011 at 11:11 PM.
- 04-10-2011, 02:00 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
I'm not so sure about that; the assignment explicitly states that any number will do as long as it is in the range [0, 100]. Anticipating for a more general situation is fine as long as it is used. The solution to the assignment problem will (most likely) never use another interval.
In general: anticipating for possible future cases causes (a bit of) code bloat and can be added later when needed. I often find myself removing lots of (small) methods that could've been handy but are never used although they could've been useful. I don't know where to draw the line here ... Generalizing can be fun now and then but its use is limited in my experience.
kind regards,
Jos (<--- your free Sunday's philosopher ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Exception Handling
By eLancaster in forum New To JavaReplies: 4Last Post: 02-20-2011, 12:00 AM -
Exception Handling in Struts 2
By mrunalcavle in forum Advanced JavaReplies: 0Last Post: 01-10-2011, 07:04 AM -
Exception Handling
By liljester in forum New To JavaReplies: 4Last Post: 06-21-2010, 03:09 PM -
Exception Handling help
By MZA in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:23 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks