public class BMINaiveErrorCheck
1: //BMINaiveErrorCheck.java
3: import java.util.Scanner;
5: public class BMINaiveErrorCheck
6: {
7: public static void main(String[] args)
8: {
9: Scanner scnr = new Scanner(System.in);
10: int weightVal; // User defined weight (lbs)
11: int heightVal; // User defined height (in)
12: float bmiCalc; // Resulting BMI
13: char quitCmd; // Indicates quit/continue
15: weightVal = 0;
16: heightVal = 0;
17: quitCmd = 'a';
19: while (quitCmd != 'q')
20: {
21: // Get user data
22: System.out.print("Enter weight (in pounds): ");
23: weightVal = scnr.nextInt();
25: // Error checking, non-negative weight
26: if (weightVal < 0)
27: {
28: System.out.println("Invalid weight.");
29: }
30: else
31: {
32: System.out.print("Enter height (in inches): ");
33: heightVal = scnr.nextInt();
34: // Error checking, non-negative height
36: if (heightVal < 0)
37: {
38: System.out.println("Invalid height.");
39: }
40: }
42: // Calculate BMI and print user health info if no input error
43: // Source: http://www.cdc.gov/
44: if ((weightVal <= 0) || (heightVal <= 0))
45: {
46: System.out.println("Cannot compute info.");
47: }
48: else
49: {
50: bmiCalc = ((float) weightVal /
51: (float)(heightVal * heightVal)) * 703.0f;
53: System.out.println("BMI: " + bmiCalc);
54: System.out.println("(CDC: 18.6-24.9 normal)");
55: // Source: http://www.cdc.gov/
56: }
58: // Prompt user to continue/quit
59: System.out.print("\nEnter any key ('q' to quit): ");
60: quitCmd = scnr.next().charAt(0);
61: }
62: }
63: }