Go Back   Java Programming Forums > Java Standard Edition Programming Help > Exceptions


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-12-2009, 08:48 PM
Junior Member
 

Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Hax007 is on a distinguished road
Default 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.

Java Code
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.

Java Code
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.

Java 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: I figured it out..




Last edited by Hax007; 08-12-2009 at 11:01 PM.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 10-12-2009, 07:53 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: Exception-Based Problem

Good for you, I'm not really sure how your code works and exceptions is something you need to include in your design from the beginning and decide how you want the flow to be.

For instance you might want to create your own exceptions like InvalidIndicatorException which would be thrown if am/pm was missing or is wrong.

You could on the other hand just decide to use the IllegalArgumentException and throw that with a message of what went wrong or simply just handle your input parsing and output the message to the user straight away, no exceptions thrown or anything.

It all depends on the project in hand and what your needs are for handling errors and exceptions.

// Json
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
ymsg exception problem maruf10 Java Networking 0 19-09-2009 06:05 PM
Help with ClassNotFound Exception multicoder Exceptions 3 01-07-2009 07:58 PM
COM based component Project jazz2k8 Java Native Interface 1 08-10-2008 03:54 AM
Exception handling AnithaBabu1 Exceptions 6 27-08-2008 02:37 PM
Funny Java based animation JavaPF The Cafe 1 07-06-2008 11:55 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:05 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.