w3resource

Java BMI Calculator: Calculate Your Body Mass Index (BMI)

BMI Calculator - Simple BMI Calculator:

BMI Calculator :

Calculate Body Mass Index based on user input for weight and height.

The Body Mass Index (BMI) calculator takes user input for weight and height and calculates the BMI using a formula. Based on the result, it classifies the user as underweight, normal weight, overweight, or obese.

Input: Weight (kg) and height (m).
Output: BMI value and corresponding category (e.g., underweight, normal, overweight).

Example:

  • Input: Weight = 70 kg, Height = 1.75 m
  • Output: BMI = 22.86 (Normal weight)

Solution 1: BMI Calculator using Basic Java

Code:

import java.util.Scanner;

public class BMICalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Initialize scanner to take user input

        // Take user input for weight in kilograms
        System.out.println("Enter your weight in kilograms (kg): ");
        double weight = scanner.nextDouble(); 

        // Take user input for height in meters
        System.out.println("Enter your height in meters (m): ");
        double height = scanner.nextDouble(); 

        // Calculate BMI using the formula: BMI = weight / (height * height)
        double bmi = weight / (height * height);

        // Display the calculated BMI
        System.out.printf("Your BMI is: %.2f\n", bmi);

        // Determine the BMI category based on the value
        if (bmi < 18.5) {
            System.out.println("You are underweight.");
        } else if (bmi >= 18.5 && bmi < 24.9) {
            System.out.println("You have a normal weight.");
        } else if (bmi >= 25 && bmi < 29.9) {
            System.out.println("You are overweight.");
        } else {
            System.out.println("You are obese.");
        }

        scanner.close(); // Close the scanner
    }
} 

Output:

Enter your weight in kilograms (kg): 
 76
Enter your height in meters (m): 
 2.2
Your BMI is: 15.70
You are underweight.

Explanation :

  • Input Handling: Takes weight and height from the user using the Scanner class.
  • BMI Calculation: Computes BMI using the formula BMI = weight / (height * height).
  • Result Formatting: Displays BMI to two decimal places using printf.
  • Categorization: Uses conditional statements to determine and display the BMI category.
  • Output: Prints the BMI value along with the corresponding weight category (underweight, normal, overweight, or obese).

Solution 2: BMI Calculator with Error Handling and Improved Input Validation

Code:

import java.util.Scanner;

public class BMICalculatorWithValidation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Initialize scanner for user input
        double weight = 0, height = 0;

        // Input weight with validation
        while (true) {
            System.out.println("Enter your weight in kilograms (kg): ");
            if (scanner.hasNextDouble()) {
                weight = scanner.nextDouble();
                if (weight > 0) {
                    break; // Valid weight entered
                } else {
                    System.out.println("Weight must be greater than zero.");
                }
            } else {
                System.out.println("Please enter a valid number for weight.");
                scanner.next(); // Clear the invalid input
            }
        }

        // Input height with validation
        while (true) {
            System.out.println("Enter your height in meters (m): ");
            if (scanner.hasNextDouble()) {
                height = scanner.nextDouble();
                if (height > 0) {
                    break; // Valid height entered
                } else {
                    System.out.println("Height must be greater than zero.");
                }
            } else {
                System.out.println("Please enter a valid number for height.");
                scanner.next(); // Clear the invalid input
            }
        }

        // Calculate BMI using the formula
        double bmi = weight / (height * height);

        // Display the BMI value
        System.out.printf("Your BMI is: %.2f\n", bmi);

        // Display BMI category based on the BMI value
        if (bmi < 18.5) {
            System.out.println("You are underweight.");
        } else if (bmi >= 18.5 && bmi < 24.9) {
            System.out.println("You have a normal weight.");
        } else if (bmi >= 25 && bmi < 29.9) {
            System.out.println("You are overweight.");
        } else {
            System.out.println("You are obese.");
        }

        scanner.close(); // Close the scanner
    }
}  

Output:

Enter your weight in kilograms (kg): 
 76
Enter your height in meters (m): 
 1.9
Your BMI is: 21.05
You have a normal weight.

Explanation:

  • Input Validation: Ensures valid numeric input for weight and height, prompting the user to re-enter values if invalid.
  • Error Handling: Verifies that both weight and height are positive values before proceeding with BMI calculation.
  • BMI Calculation: Uses the same formula as the first solution to compute BMI.
  • Result Output: Displays BMI to two decimal places and the corresponding weight category.
  • User-Friendly: Provides clear error messages and handles invalid input gracefully, improving user experience.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-bmi-calculator.php