Results 1 to 4 of 4
Thread: Throw and catches
- 11-22-2010, 04:32 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 11
- Rep Power
- 0
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; } } } }
- 11-22-2010, 04:47 PM #2
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?
- 11-23-2010, 01:38 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 11
- Rep Power
- 0
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
- 11-23-2010, 01:36 PM #4
Similar Threads
-
where to use throw and throws
By javastuden in forum New To JavaReplies: 4Last Post: 11-02-2010, 03:31 PM -
About throw message
By killerf2006 in forum New To JavaReplies: 4Last Post: 08-22-2010, 11:48 AM -
what exception to throw
By DoolinDalton in forum New To JavaReplies: 5Last Post: 02-10-2010, 03:45 PM -
throw exception
By GIRISH PATEL in forum New To JavaReplies: 4Last Post: 04-23-2009, 04:35 AM -
throw an exception
By sfe23 in forum New To JavaReplies: 3Last Post: 02-14-2009, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks