Results 1 to 4 of 4
  1. #1
    cdawg_2010 is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default Throw and catches

    I wrote this program for converting feet and inches entered in to centimeters, but i need help with the catch part. the assignment says that if a user enters a negative or non-digit number, the user should be prompted to enter another number. I think i've just been working on this for too long and i'm overlooking a simple mistake.
    Java Code:
       import java.util.Scanner;
       public class Assignment21a {
       
       
          public static void main(String[] args) {
          
             System.out.println(
                "To exit please type any non-number.");
          
             while (true)
             {
                Scanner input = new Scanner(System.in);
                System.out.print("Please enter feet then inches to be converted: ");
                try {
                   int feet = input.nextInt();
                   int inches = input.nextInt();
                   double conversion = 2.54;
                
                   int totalInches = ( feet * 12);
                   double totalCentimeters = (double) ((totalInches + inches) * conversion);
                     
                   System.out.println( totalCentimeters + " centimeters");
                }
                   catch (Exception e)
                   {
                      System.out.println("Thanks for using the centimeter converter.");
                      break;
                   }
             }
          }
       }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default

    Why bother with the catch to accomplish your goal? What exception would be thrown for a negative number?

    Hint: What do you want to do while the user enters negative numbers?

  3. #3
    cdawg_2010 is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default

    well our instructor wants us to throw it and suggested using a catch too so thats why i had that in there. its part of the assignment. but i'll think about what you said when looking at it

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

Similar Threads

  1. where to use throw and throws
    By javastuden in forum New To Java
    Replies: 4
    Last Post: 11-02-2010, 04:31 PM
  2. About throw message
    By killerf2006 in forum New To Java
    Replies: 4
    Last Post: 08-22-2010, 12:48 PM
  3. what exception to throw
    By DoolinDalton in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 04:45 PM
  4. throw exception
    By GIRISH PATEL in forum New To Java
    Replies: 4
    Last Post: 04-23-2009, 05:35 AM
  5. throw an exception
    By sfe23 in forum New To Java
    Replies: 3
    Last Post: 02-14-2009, 05:41 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
  •