When InputMismatchException is thrown I enter an infinite loop and I can not for the life of me figure out why. Basically the main goal of this program is to throw an exception for a negative number entered by the user and to make sure that the user actually enters an integer (not something like "r45"). Any help would be greatly appreciated. Thank you.
import java.util.*;
public class conversion{
static Scanner in = new Scanner (System.in);
static final double centimeters_per_inch = 2.54;
static final int inches_per_foot = 12;
public static void main (String [] args){
int feet;
int inches;
int totalInches;
double centimeters;
boolean done = false;
do{
try
{
System.out.print("Enter feet: ");
System.out.flush();
feet = in.nextInt();
System.out.println();
System.out.print("Enter inches: ");
System.out.flush();
inches = in.nextInt();
if (feet < 0 || inches < 0)
throw new NonNegative();
System.out.println();
done = true;
System.out.println("The numbers you entered are " + feet +" feet and " + inches+ " inches");
totalInches = inches_per_foot * feet + inches;
System.out.println();
System.out.println("The total number of inches = " + totalInches);
centimeters = totalInches * centimeters_per_inch;
System.out.println("The number of centimeteres = " + centimeters);
}
catch (NonNegative a){
System.out.println(a.toString());
}
catch(InputMismatchException e) {
System.out.println("This is not a number");
}
}while(!done);
}
}