-
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.
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;
}
}
}
}
-
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?
-
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
-