This program calculates BMI: import java.util.Scanner; public class BodyMassInde
ID: 3627839 • Letter: T
Question
This program calculates BMI:import java.util.Scanner;
public class BodyMassIndex
{
public static void main(String argv[])
{
Scanner input = new Scanner(System.in);
String inputString;
double bmi;
double pounds;
double inches;
System.out.print("Please enter height (in inches) ");
inputString = input.nextLine();
try
{
Double.parseDouble(inputString);
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid inch value. Number must be a decimal.");
System.out.print("Re-enter height in inches: ");
inputString = input.nextLine();
}
inches=Double.parseDouble(inputString);
if(inches<=0)
{
System.out.println("Invalid inch value. Must be a positive number.");
System.out.print("Re-enter height in inches:");
inputString = input.nextLine();
inches=Double.parseDouble(inputString);
}
System.out.print("enter weight(in pounds) ");
inputString = input.nextLine();
try
{
Double.parseDouble(inputString);
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid pound value. Number must be a decimal.");
System.out.print("Re-enter the weight in pounds: ");
inputString = input.nextLine();
}
pounds=Double.parseDouble(inputString);
if(pounds <= 0)
{
System.out.println("Invalid pound value. Must be a positive number.");
System.out.print("Re-enter the weight in pounds:");
inputString = input.nextLine();
pounds=Double.parseDouble(inputString);
}
System.out.println("Height = " + inches + """);
System.out.println("Weight = " + pounds + " pounds");
System.out.printf("Body Mass Index = %.1f ",(pounds * 703.)/(inches * inches));
}//end main
}//end BodyMassIndexClass
However I need the program to calculate BMI taking Feet and inches to produce a sample session like this:
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
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.
Is there a way to get the program to do that and make it similar to the above program?
Explanation / Answer
import java.util.Scanner;
public class BodyMassIndex
{
public static void main(String argv[])
{
//declaring scanner object
Scanner input = new Scanner(System.in);
//variable declaration
double bmi;
String inputStr;
double pounds;
double inches;
int feets;
System.out.print("Please enter height ( feets and inches) ");
inputStr=input.nextLine();
feets= Integer.parseInt(inputStr.substring(0,1));
System.out.println(feets);
inches=Double.parseDouble
(inputStr.substring(2,inputStr.length()));
while(inches<0||feets<0)
{
System.out.print("Please enter height(feets and inches) ");
inputStr=input.nextLine();
feets= Integer.parseInt(inputStr.substring(0,1));
inches=Double.parseDouble
(inputStr.substring(2,inputStr.length()));
}
double total=inches+(feets*12);
System.out.print("enter weight(in pounds) ");
pounds= input.nextDouble();
while(pounds<0)
{
System.out.println("Invalid pound value. Number must be a decimal.");
System.out.print("Re-enter the weight in pounds: ");
pounds = input.nextDouble();
}
System.out.println("Height = " + feets + "'-"+inches+"''");
System.out.println("Weight = " + pounds + " pounds");
System.out.println("Body Mass Index = "+(pounds * 703)/(total * total));
}//end main
}//end BodyMassIndexClass
Please enter height ( feets and inches) 5 9.25
enter weight(in pounds) 150.5
Height = 5'-9.25''
Weight = 150.5 pounds
Body Mass Index = 22.062375373066246
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.