Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Exception-Based Problem

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

    Enter h:m:AM or PM > [b]test[/b]
    Non-numeric Data!
     
    Enter h:m:AM or PM > [b]15:5:AM[/b]
    Values out of range
     
    Enter h:m:AM or PM > [b]1:15:am[/b]
    75
     
    Enter h:m:AM or PM > [b]12:10:am[/b]
    10
     
    Enter h:m:AM or PM > [b]1:5:tm[/b]
    Invalid AM/PM indicator!
     
    Enter h:m:AM or PM > [b]11:50:am[/b]
    710
     
    Enter h:m:AM or PM > [b]12:10:pm[/b]
    730
     
    Enter h:m:AM or PM > [b]1:25:PM[/b]
    805
     
    Enter h:m:AM or PM > [b]11:50:pm[/b]
    1430
     
    Enter h:m:AM or PM > [b]12:55:am[/b]
    55
     
    Enter h:m:AM or PM > [b]5[/b]
    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.

    Enter h:m:AM or PM > [b]test[/b]
    Non-numeric Data!
     
    Enter h:m:AM or PM > [b]15:5:AM[/b]
    Values out of range
     
    Enter h:m:AM or PM > [b]1:15:am[/b]
    75
     
    Enter h:m:AM or PM > [b]12:10:am[/b]
    10
     
    Enter h:m:AM or PM > [b]1:5:tm[/b]
    Invalid AM/PM indicator!
     
    Enter h:m:AM or PM > [b]11:50:am[/b]
    710
     
    Enter h:m:AM or PM > [b]12:10:pm[/b]
    730
     
    Enter h:m:AM or PM > [b]1:25:PM[/b]
    805
     
    Enter h:m:AM or PM > [b]11:50:pm[/b]
    1430
     
    Enter h:m:AM or PM > [b]12:55:am[/b]
    55
     
    Enter h:m:AM or PM > [b]5[/b]
    [u]Missing Colon[/u]

    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.

    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; December 8th, 2009 at 07:01 PM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    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

Similar Threads

  1. ymsg exception problem
    By maruf10 in forum Java Networking
    Replies: 0
    Last Post: September 19th, 2009, 01:05 PM
  2. Error of "ClassNotFound Exception"
    By multicoder in forum Exceptions
    Replies: 3
    Last Post: July 1st, 2009, 02:58 PM
  3. Com based component project using Java
    By jazz2k8 in forum Java Native Interface
    Replies: 1
    Last Post: October 7th, 2008, 10:54 PM
  4. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM
  5. How to make java based animation?
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: June 7th, 2008, 06:55 AM