Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default Try / Catch Problems

    Hey Guys / Gals,

    I am trying to use try / catch in a method but I seem to have run into problems. I don't think I'm too far away from the final solution. If you can help I would be grateful - the problem is that the Java materials that I have only show examples of try / catch with while loops and not for methods that use for.

    The code is supposed to calculate the distance travelled over a period of 5 days, each time a distance is reached it should print the distance travelled on the day, and the total distance. The problem is that if users try to input the letter 4 rather than 4 you get a java.lang.NumberFormatException.

    I am trying to use try / catch to resolve this.

    My code:

    Java Code:
       public void calculate()
       {
          String string;
          int meters = 0;
          int total = 0;
          
          for (int day = 0; day < 5; day++)
          {
             string = Dialog.request("Enter distance " + (day + 1), "");
             
             try
             {
             meters = Integer.parseInt(string);
             total = total + meters;
             System.out.println("On day " + (day + 1) + " you travelled " + distance
                                 + " meters, it has travelled " + total
                                 + " in total");
             }
             catch (NumberFormatException anException)
             {
                Dialog.alert("Thats not a number!");
                string = Dialog.request("Enter distance " + (day + 1), "");
             }         
          }
       }
    The problem is that since I have added the try / catch exception to the code, although it does recognise that I have entered a string rather than int, it does not seem to calculate the correct number. Essentially, I want the code if a string has been entered incorrectly, to prompt the user again for the distance as a number.

    E.g

    program:Distance travelled? (Day 1)

    user: four

    program: Thats not a number!

    program: Distance travelled? (Day 1) .... not Day 2.

    If you require any further information please shout.

    All the best,

    FMJ

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    in the catch clause, put --day (or day--) at the end. Also, you may want to change your loop to for(int day=1; d<=5; ++day) and change all the (day +1)s to day to make the code more readable.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  2. try catch...
    By MarkWilson in forum New To Java
    Replies: 8
    Last Post: 06-27-2008, 05:39 PM
  3. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM
  4. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 04:10 PM
  5. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 08:52 PM

Posting Permissions

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