Results 1 to 3 of 3
Thread: Exception-Based Problem
- 12-08-2009, 10:08 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
[Solved] Exception-Based Problem
I was assigned the task of making a program that outputs the number of minutes that have passed since midnight. The program reads the input from the user as h:m:ind, where h is the hour (must be >0 and <=12), m is the minuted (must be >= 0 and <60), and ind is either AM or PM.
Below is ten sample runs of what the program should give the following output.
User input is in bold.
If the entry is valid the program outputs the amount of minutes and ends, otherwise it does one of the following...Enter h:m:AM or PM > test
Non-numeric Data!
Enter h:m:AM or PM > 15:5:AM
Values out of range
Enter h:m:AM or PM > 1:15:am
75
Enter h:m:AM or PM > 12:10:am
10
Enter h:m:AM or PM > 1:5:tm
Invalid AM/PM indicator!
Enter h:m:AM or PM > 11:50:am
710
Enter h:m:AM or PM > 12:10:pm
730
Enter h:m:AM or PM > 1:25:PM
805
Enter h:m:AM or PM > 11:50:pm
1430
Enter h:m:AM or PM > 12:55:am
55
Enter h:m:AM or PM > 5
Values out of range
"Values out of range!" if h or m is outside its legal range.
"Missing colon!" if a colon is missing.
"Non-numeric data!" if h or m is not an integer.
"Invalid AM/PM indicator" if the indicator is neither AM nor PM (upper or lower)
The hardest part of the assignment is that this program must be executed without any validation and without using any if statements, switch statements or similar construct.
Below is my programs output.
Incorrect outputs are underlined.
I believe the source of the problem is RuntimeException. Since it is a superclass, I believe NoSuchElement is being affected, though I could be wrong.Enter h:m:AM or PM > test
Non-numeric Data!
Enter h:m:AM or PM > 15:5:AM
Values out of range
Enter h:m:AM or PM > 1:15:am
75
Enter h:m:AM or PM > 12:10:am
10
Enter h:m:AM or PM > 1:5:tm
Invalid AM/PM indicator!
Enter h:m:AM or PM > 11:50:am
710
Enter h:m:AM or PM > 12:10:pm
730
Enter h:m:AM or PM > 1:25:PM
805
Enter h:m:AM or PM > 11:50:pm
1430
Enter h:m:AM or PM > 12:55:am
55
Enter h:m:AM or PM > 5
Missing Colon!
Perhaps there is a better way to throw invalid without using if statements. I thought of using a regex, i.e.
However, I couldn't find an exception to throw if it was wrong. Does anyone know if it's possible? If it's not, then it's just a matter of trying to configure the Toolbox.crash to be caught by the RuntimeErrorException rather then the NoSuchElementException (which I still haven't the slightest clue how to do).Java Code:String regex = "(1[012]|[1-9]):[0-5][0-9]:(AM|PM|am|pm)";
Handling exceptions is a very hard/new topic for me so any help would be appreciated, thanks.
EDIT: Thanks for the help guys, but I figured it.Last edited by Hax007; 12-09-2009 at 12:39 AM.
- 12-08-2009, 10:58 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
First off I do not see what the problem is, since "5" is missing a colon, should't u say that then?
But incase u want the other error instead, then try change their position:
Maybe that will help?Java Code:catch (RuntimeException e) { out.println("Values out of range!"); } catch (NoSuchElementException e) { out.println("Missing colon!"); }
- 12-08-2009, 11:17 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
While you are right that it is correct to say that it is indeed an error, the instructions specifically state that "Values out of range!" must be outputed in this instance.
Also, you cannot switch them around because you get the following compiling error.
This, as I stated before, is due to the fact that RunTimeErrorException is a super class of NoSuchElementException.Java Code:java.util.NoSuchElementException has already been caught catch (NoSuchElementException e)
Similar Threads
-
i need an example of JSR179 ((Location based Ser)implementation for CDC based device
By talk_to_vivekmishra in forum CDC and Personal ProfileReplies: 3Last Post: 12-30-2010, 10:07 AM -
Problem using EBI Webservice getting Exception
By tealc76 in forum Advanced JavaReplies: 0Last Post: 11-21-2009, 06:20 PM -
Problem with null pointer exception?
By dunafrothint in forum AWT / SwingReplies: 3Last Post: 11-11-2009, 03:34 PM -
Entity - Field-Based Access Vs Property Based Access
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 3Last Post: 11-02-2009, 07:18 PM -
difference between code based security and role based security
By boy22 in forum New To JavaReplies: 1Last Post: 07-23-2007, 11:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks