# Exception-Based Problem

• 12-08-2009, 11:08 PM
Hax007
[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.

Quote:

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
If the entry is valid the program outputs the amount of minutes and ends, otherwise it does one of the following...

"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.

Quote:

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!
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.

Perhaps there is a better way to throw invalid without using if statements. I thought of using a regex, i.e.

Code:

`String regex = "(1[012]|[1-9]):[0-5][0-9]:(AM|PM|am|pm)";`
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).

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.
• 12-08-2009, 11:58 PM
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:
Code:

```catch (RuntimeException e)             {  out.println("Values out of range!");             } catch (NoSuchElementException e)             {  out.println("Missing colon!");             }```
Maybe that will help?
• 12-09-2009, 12:17 AM
Hax007
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.

Code:

```java.util.NoSuchElementException has already been caught             catch (NoSuchElementException e)```
This, as I stated before, is due to the fact that RunTimeErrorException is a super class of NoSuchElementException.