Results 1 to 3 of 3
  1. #1
    Hax007 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default [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.

    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.

    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: Thanks for the help guys, but I figured it.
    Last edited by Hax007; 12-09-2009 at 01:39 AM.

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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:
    Java Code:
    catch (RuntimeException e)
                {  out.println("Values out of range!");
                }
    catch (NoSuchElementException e)
                {  out.println("Missing colon!");
                }
    Maybe that will help?

  3. #3
    Hax007 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    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.

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

Similar Threads

  1. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 11:07 AM
  2. Problem using EBI Webservice getting Exception
    By tealc76 in forum Advanced Java
    Replies: 0
    Last Post: 11-21-2009, 07:20 PM
  3. Problem with null pointer exception?
    By dunafrothint in forum AWT / Swing
    Replies: 3
    Last Post: 11-11-2009, 04:34 PM
  4. Entity - Field-Based Access Vs Property Based Access
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 11-02-2009, 08:18 PM
  5. Replies: 1
    Last Post: 07-24-2007, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •