This is my first program so no doubt I've made mistakes. The code works and meets all requirements as far as I can see.
(Further info: program to calculate Body Mass Index (BMI) allowing user to enter weight in stones and pounds or kilograms It allows user to enter height in feet and inches or centimeters.
It then calculates BMI and displays the result.
I'm hoping to get a critical review of the code to identify any potential problems with it. I am open to any criticism and any suggestions on how I can improve the code and improve the overall quality.
import java.util.Scanner;
import java.util.InputMismatchException;
public class BmiCalculatorWorking
{
static double totalIn;
static double kgs;
static double totalPound;
static double pounds;
static int stone;
static int feet;
static int inches;
static double cms;
static double totalCm;
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
boolean continueLoop = true;
do
{
try
{
System.out.println("Welcome to the BMI Calculator!\nYou"+
" may calculate your BMI in 1 of 2 ways:\n\n1. by"+
" entering Weight in stone and pounds and Height"+
" in feet and inches (Imperial) \nor\n2. by"+
" entering Weight in Kilograms and Height"+
" in Meters (Metric)");
while (true)
{
System.out.print("\nEnter 1 for Imperial OR Enter 2 for"+
" Metric: ");
int userInput;
userInput = input.nextInt();
if (userInput == 1)
{
bmiCalcImperial();
break;
}
else if (userInput == 2)
{
bmiCalcMetric();
break;
}
else
{
System.out.println("Invalid Input, please"+
" enter either 1 or 2");
continue;
}
}
continueLoop = false;
}
catch ( InputMismatchException inputMismatchException)
{
System.err.printf( "\nException: %s\n", inputMismatchException );
input.nextLine();
System.out.println("Input invalid! You must enter"+
" numbers only. Please try again.\n" );
}
}while ( continueLoop );
}
public static void bmiCalcImperial()
{
Scanner input = new Scanner( System.in );
boolean continueLoop = true;
//ENTER WEIGHT IN STONE AND/OR POUNDS within range AND CONVERT TO KGS
do{
System.out.print("\nPlease enter your weight in Stone: ");
try{
stone = input.nextInt();
while (stone < 3 || stone > 30)
{
System.out.print("Please enter a valid weight (between 3 and"+
" 30 stone):");
stone = input.nextInt();
}
continueLoop = false;
}
catch ( InputMismatchException inputMismatchException)
{
System.err.printf( "\nException: %s\n", inputMismatchException );
input.nextLine();
System.out.println("Input invalid! You must enter"+
" (St)numbers only. Please try again.\n" );
}
}while ( continueLoop );
continueLoop = true;
do{
System.out.print("Please enter your weight in Pounds: ");
try {
pounds = input.nextDouble();
while (pounds < 0 || pounds > 400)
{
System.out.print("Please enter a valid weight"+
" (between 0 and 400 pounds):");
pounds = input.nextDouble();
}
continueLoop = false;
}
catch ( InputMismatchException inputMismatchException)
{
System.err.printf( "\nException: %s\n", inputMismatchException );
input.nextLine();
System.out.println("Input invalid! You must enter"+
" (Ibs)numbers only. Please try again.\n" );
}
}while ( continueLoop );
totalPound = stone*14 + pounds;
kgs = totalPound*0.45359237;
System.out.printf("Your weight in Kilograms is: %.3f", kgs);
System.out.print(" Kgs\n");
continueLoop = true;
//ENTER HEIGHT IN FEET AND/OR INCHES within range AND CONVERT TO CMS
do{
try{
System.out.print("\nPlease enter your Height in Feet: ");
feet = input.nextInt();
while (feet < 3 || feet > 7)
{
System.out.print("Please enter a valid Height (between"+
" 3 and 7 Feet):");
feet = input.nextInt();
}
continueLoop = false;
}
catch ( InputMismatchException inputMismatchException)
{
System.err.printf( "\nException: %s\n", inputMismatchException );
input.nextLine();
System.out.println("Input invalid! You must enter (FEET)"+
"numbers only. Please try again.\n" );
}
}while ( continueLoop );
continueLoop = true;
do{
try{
System.out.print("Please enter your Height in Inches: ");
inches = input.nextInt();
while (inches < 0 || inches > 84)
{
System.out.print("Please enter a valid Height (between"+
" 0 and 84 inches):");
inches = input.nextInt();
}
continueLoop = false;
}
catch ( InputMismatchException inputMismatchException)
{
System.err.printf( "\nException: %s\n", inputMismatchException );
input.nextLine();
System.out.println("Input invalid! You must enter"+
" (Inches)numbers only. Please try again.\n" );
}
}while ( continueLoop );
//converts feet and Inches to total Inches and then converts to centimetres
totalIn = feet*12 + inches;
totalCm = totalIn*2.54;
System.out.printf("Your height in Centimeters is: %.0f", totalCm);
System.out.print(" cms\n");
//Calculate BMI by converting from Imperial user input to metric
//and then using the metric formula
double ImperialToMetricBmi;
double InchesToMtrHeight;
InchesToMtrHeight = totalIn*2.54/100;
ImperialToMetricBmi = (kgs/(InchesToMtrHeight*InchesToMtrHeight));
System.out.printf("\nYour (converted to use Metric"+
" formula) BMI is: %.3f\n" ,ImperialToMetricBmi);
//Display BMI description based on Imperial user input
//calcualted with metric formula
if(ImperialToMetricBmi < 18.5)
{
System.out.println("You are underweight and are at risk of "+
"developing problems such as nutritional deficiency and "+
"osteoporosis.\nIt is recommended you seek professional"+
" medical advice.");
}
else
if (ImperialToMetricBmi >= 18.5 && ImperialToMetricBmi <= 24.9)
{
System.out.println("You are a healthy, normal weight");
}
else
if (ImperialToMetricBmi >= 25 && ImperialToMetricBmi <= 29.9)
{
System.out.println("You are overweight and are at"+""
+ " moderate risk of developing"+"\n"
+ " heart disease, high blood pressure,"+
" stroke, diabetes.\n It is recommended "+
"you lose weight through exercise and"+
" a healthy diet.");
}
else
if (ImperialToMetricBmi > 30 )
{
System.out.println("You are obese and are at"+
" High risk of developing heart"+
" disease, high blood pressure,"+
" stroke, diabetes. It is recommended"+
" you seek professional"+
" medical advice. ");
}
}
public static void bmiCalcMetric()
{
Scanner input = new Scanner(System.in);
boolean continueLoop = true;
//ENTER WEIGHT IN KGS AND CONVERT TO STONE AND POUNDS
double mtr;
double bmi;
do{
try{
System.out.print("\nPlease enter your weight in Kilograms: ");
kgs = input.nextDouble();
while (kgs < 20 || kgs > 180)
{
System.out.print("Please enter a valid weight "+
"(between 20 and 180kgs):");
kgs = input.nextDouble();
}
continueLoop = false;
}
catch ( InputMismatchException inputMismatchException)
{
System.err.printf( "\nException: %s\n", inputMismatchException );
input.nextLine();
System.out.println("Input invalid! You must enter (KGS)"+
"numbers only. Please try again.\n" );
}
}while (continueLoop);
//note: Converts kgs to ibs **But not St AND Ibs !!**
pounds = kgs*2.204622;
System.out.printf("Your weight in pounds is: %.0f" ,pounds);
System.out.print(" Ibs\n");
continueLoop = true;
do{
try{
System.out.print("\nPlease enter your Height in Centimeters: ");
cms = input.nextInt(); //should this be .nextDouble?
while (cms < 91 || cms > 213)
{
System.out.print("Please enter a valid Height (between"+
" 91 and 213cms):");
cms = input.nextDouble();
}
continueLoop = false;
}
catch ( InputMismatchException inputMismatchException)
{
System.err.printf( "\nException: %s\n", inputMismatchException );
input.nextLine();
System.out.println("Input invalid! You must enter (cms)"+
"numbers only. Please try again.\n" );
}
}while (continueLoop);
totalIn = cms/2.54;
System.out.printf("\nYour height in inches is: %.0f" ,totalIn);
System.out.print( "\" \n");
//Calculate BMI
mtr = cms/100;
bmi = (kgs/(mtr*mtr));
//Display BMI
System.out.printf("\nYour BMI is: %.3f\n" ,bmi);
//Display BMI description
if(bmi < 18.5)
{
System.out.println("\nYou are underweight");
}
else
if (bmi >= 18.5 && bmi <= 24.999)
{
System.out.println("\nYou are a healthy, normal weight");
}
else
if (bmi >= 25 && bmi <= 29.999)
{
System.out.println("\nYou are overweight");
}
else
if (bmi > 30 )
{
System.out.println("\nYou are obese");
}
}
}