Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Perform input validation by making sure that the user enters a positive integer

ID: 3627812 • Letter: P

Question

Perform input validation by making sure that the user enters a positive integer number for feet, a nonnegative decimal number for inches, and a positive decimal number for weight. See the sample session for details. In particular, note the format for the echo-printed height and weight values and for the generated body-mass-index value.


bmi = 704 x weightInPounds / heightIn Inches ^2


Sample session Output:

Enter height using feet space inches (e.g., 5 6.25): hi there
Invalid feet value. Must be an integer.
Invalid inches value. Must be a decimal number.
Re-enter height using feet space inches (e.g., 5 6.25): 0 9
Invalid feet value. Must be positive.
Re-enter height using feet space inches (e.g., 5 6.25): 5.25 0
Invalid feet value. Must be an integer.
Re-enter height using feet space inches (e.g., 5 6.25): 5 9.25
Enter weight in pounds: 0
Invalid pounds value. Must be positive.
Re-enter weight in pounds: 150.5

height = 5'-9.25"
weight = 150.5 pounds
body mass index = 22.1

Explanation / Answer

//ok, some lines of code will not fit into one line of cramster. so copy this code in your text editor and

//make it look nice. I used java 6 to make and test this file.

//run it by typing the command : java BMI

//PLEASE RATE ! I SPENT A LOT OF TIME MEETING

//YOUR UNUSUAL REQUIREMENTS !!!

import java.util.*;


public class BMI
{
    static     int ht = 0;
    static float wt = 0.0f;
   
    public static void main(String[]args)
    {
        System.out.println("Welcome to Body Mass Index (BMI) Calculator !");   
        Scanner in = new Scanner(System.in);
       
        String height = "";
        String weight = "";
        float bmi = 0.0f;
       
        boolean hIsOk = false;
        boolean wIsOk = false;
       
        while(!hIsOk)
        {

            System.out.println(" Enter height using feet space inches");
            height = in.nextLine();
            hIsOk = checkHeight(height);
        }
       
       
        while(!wIsOk)
        {

            System.out.println(" Enter weight in pounds !");
            weight = in.nextLine();
            wIsOk = checkWeight(weight);
        }
       
        bmi = (704 * wt) / (ht * ht);
       
        System.out.println(" height : " + height);
        System.out.println("weight : " + weight);
        System.out.printf("body mass index : %3.1f ", bmi);
       
    }//main

    public static boolean checkHeight(String h)
    {
        String [] data = h.split(" ");
        boolean ok = false;
        int feet = 0;
        Float inch = 0.0f;
       
        try
        {
            try
            {
                feet = Integer.parseInt(data[0]);
                if(feet <= 0)
                {
                    System.out.println("Invalid feet value. Must be positive.");
                    return ok;
                }
               
            }catch(NumberFormatException e)
            {
                System.out.println("Invalid feet value. Must be an integer. ");
                return ok;
            }   
           
            try
            {
                inch = Float.parseFloat(data[1]);
                if(inch < 0||inch > 12)
                {
                    System.out.println("Invalid inches value. Must be between 0 and 12");
                    return ok;
                }
               
            }catch(NumberFormatException e)
            {
                System.out.println("Invalid inches value. Must be a decimal number. ");
                return ok;
            }   
           
            ht = feet * 12 + inch.intValue();
            ok = true;
           
        }catch(Exception e){System.out.println("An unexpected error occured ! " + e);}
       
        return ok;
    }
   
    public static boolean checkWeight(String w)
    {
        boolean ok = false;
        try
        {
            wt = Float.parseFloat(w);
            if(wt <= 0)
                {
                    System.out.println("Invalid pounds value. Must be positive.");
                    return ok;
                }
           
            ok = true;   
           
        }catch(NumberFormatException e){System.out.println("Invalid pounds value. Must be a decimal number. ");}
       
        return ok;
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote