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 6 of 6

Thread: Exception Handling

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Exception Handling

    I'm trying to traverse a String value HH:MM:SS to determine if the colons are missing, if there are any non-numeric values, or if the values are out of range. Later in the method I also created a String array to create an object that is returned by the method. That part I've already go figure out. I need to error check the text entered which is passed to the method as a String. So what I did was create a variable newTime to hold the passed value and I plan on traversing the String till I find characters that aren't numbers. My idea is to traverse the String to find any non-numeric characters and throw an exception as you can see. It's not processing correctly when this method is called. The exception is always thrown and it looks like my for loop isn't processing properly. Any help is ap

    String COLON = ":";

    try{
    for (int i = 0; i < newTime.length(); i++) {
    if (newTime.charAt(i) != COLON.charAt(0)) {

    JOptionPane.showMessageDialog(null, "Time format must be HH:MM:SS");
    throw new InvalidTime("Time delimiter must be a \"" + COLON + "\"");
    }
    }

    } //end try block

    catch(InvalidTime ex) {

    System.out.println("Exception: " + ex + " thrown");
    } // end catch block
    Last edited by hello_world; August 4th, 2011 at 07:18 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Exception Handling

    If you are talking about something like this
    throw new SomeException();
    System.out.println("Why doesn't this line work?");
    Surely it is obvious. The print statement will never be reached because the previous line threw an exception so execution has now gone to the exception handler and will never return to the print statement.

    Why are you always throwing an exception? You should only throw an exception in exceptional circumstances. That is only when a certain condition is met using an if statement.

    Another thought, have you considered a finally clause?
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception Handling

    I'm trying to traverse a String value HH:MM:SS to determine if the colons are missing, if there are any non-numeric values, or if the values are out of range. Later in the method I also created a String array to create an object that is returned by the method. That part I've already go figure out. I need to error check the text entered which is passed to the method as a String. So what I did was create a variable newTime to hold the passed value and I plan on traversing the String till I find characters that aren't numbers. My idea is to traverse the String to find any non-numeric characters and throw an exception as you can see. It's not processing correctly when this method is called. The exception is always thrown and it looks like my for loop isn't processing properly. Any help is ap

    String COLON = ":";

    try{
    for (int i = 0; i < newTime.length(); i++) {
    if (newTime.charAt(i) != COLON.charAt(0)) {

    JOptionPane.showMessageDialog(null, "Time format must be HH:MM:SS");
    throw new InvalidTime("Time delimiter must be a \"" + COLON + "\"");
    }
    }

    } //end try block

    catch(InvalidTime ex) {

    System.out.println("Exception: " + ex + " thrown");
    } // end catch block

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Exception Handling

    The simplest and hardest way would be to use a regular expression. Simple because you could do it on one line. Hardest to get your head around it if you are not experienced with regular expressions.

    Alternatively you could use the String.split method and then validate each of the individual components (hour/minute/second).
    Last edited by Junky; August 4th, 2011 at 08:00 PM.
    Improving the world one idiot at a time!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception Handling

    If the format is NN:NN:NN then you could use substring to get each of the 3 groups of digits and do a parseInt to validate numberic and a charAt to check for the :

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Exception Handling

    Your logic is a bit out. You're testing every char in newTime against the first char in COLON - it would validate an input like "::::". Your technique for validating your input is ok - it's not as nice as using a regular expression or string splitting, but back in the day when we only had fixed-width fields (my lawn!), everybody did it your way. Since you know exactly what char should be at each position in your 8-char input, you could do it without a loop at all (the ugliest solution) with 8 if statements. if char at 0 is not 0-5, throw, if char at 2 is not ':' /* use apostrophes to make a constant char in Java */, throw - like that.

    You could construct some validation code that did use a for loop and an Array of Strings containing valid characters for each position in your field. That seems to be what you're attempting. In that case, you'd use a single index to pick both a character from newTime and its corresponding String containing valid chars, and use String.indexOf to check that the newTime char is in its corresponding validation String. That would work and might not look too ugly.

    I would habitually use a Pattern object (String.matches() does something similar) like Junky says for your particular problem - the expression for a fixed-width field with limited valid chars in each position is quite easy, once you get your head around the format.

Similar Threads

  1. Replies: 2
    Last Post: August 3rd, 2011, 11:13 AM
  2. Need help with exception handling for a string
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 6th, 2011, 06:59 AM
  3. [SOLVED] Embed a Jar into a website/Exception handling
    By Hallowed in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:34 AM
  4. Exception handling for TextFields
    By FretDancer69 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 16th, 2009, 07:48 AM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM