How can I calculate BMI in Java Where the weight and height is an array

write a program that calculates and displays a person's body mass index - java
java bmi if else
bmi calculator java source code gui
bmi calculator java applet

I have an assignment in Java, where I have to calculate the BMI of a particular user, in an array of users, an array of heights and an array of weights.

Below is the assignment:

  String[] names; // users names
                String[] familyNames; // familyNames
                int[] weight; // users weights in kilograms (kg)
                double[] height; // users height in meter (m)


                names = new String[] { "Alex", "Maria", "Anna", "Adam", "Sara", "Johan", "Frederik"};
                familyNames = new String[] {"Andersson", "Johansson", "Nordin", "Holmgren", "Svensson"};
                weight = new int[] {70, 50, 60, 50, 60, 87, 130 };
                height = new double[] { 1.80, 1.70, 1.57, 1.80, 1.69, 1.85, 1.85 };

        public static void calculateBMI(String name, String[] names, int[] weight, double[] height) {


                /*
                 * This method should be changed
                 * 
                 * Calculate and print out BMI (body mass index) for each user 
                 * BMI = weight in kg/height in meter * height in meter 
                 * 
                 * Check if the user is Underweight, Normal, Overweightor or Obese based on the BMI
                 * Underweight, when bmi is less than 18.5 
                 * Normal, when bmi is between 18.5 and 24.9 
                 * Overweight, when bmi is between 25 and 29.9
                 * Obese, when bmi is more than 30
                 * 
                 * 
                 * To check if a string is equal to another string use:
                 * stringVariable.equalsIgnoreCase(anotherStringVariable)
                 * 
                 */

            }

public static void calculateWeight(String name, String[] names, int[] weight, double[] height) {

        /*
         * This method should be changed
         * 
         * Calculate and print out the weight that the user has to loose or gain
         * 
         * Let's consider that the formula for ideal body weight is (Broca Index:):
         * Ideal Body Weight (kg) = (Height (cm) - 100) - ((Height (cm) - 100) x 10%)
         * 
         * 1 meter = 100 cm
         * 
         * 
         */
        //Your code starts here


    }// end calculateWeightLoss

I have tried doing this:

double userBMI = 0;
   for(int i = 0; i < weight.length; i++){
                for(int j = 0; j < height.length; j++){
                    userBMI = weight[i]/height[j] * height[j];
                }
            }

But I feel I am not on track, because the method when in use will be:

calculateBMI("Frederik",names, weight,height); // calculate user BMI
        calculateWeight("Frederik", names, weight, height); // calculate the weight loss or gain for the user
        System.out.println("");

So I have to find for example "frederik" in the array of names, and calculate his BMI. Any ideas will be aappreciated.

It print the BMI when passed to the method name match with the name in "names" array

public static void calculateBMI(String name, String[] names, int[] weight, double[] height) {

    for (int i = 0; i < names.length; i++) {
        if(names[i].equalsIgnoreCase(name))
            System.out.println(weight[i] / height[i] / height[i]);
    }
}

But it is risky because if the length of names array is bigger than weight or height it will throw ArrayIndexOutOfBoundsException but I assume that arrays have always properly length. If there is possibility that the length of these arrays might be different I suggest to add some conditions

And the second method:

public static void calculateWeight(String name, String[] names, int[] weight, double[] height) {

    for (int i = 0; i < names.length; i++) {
        if (names[i].equalsIgnoreCase(name)) {
            double perfectWeight = ((height[i] * 100) - 100) - (((height[i] * 100) - 100) / 10);
            System.out.println("Perfect weight for that height is : " + perfectWeight);
            if(weight[i] == perfectWeight)
                System.out.println("Person has perfect weight");
            else if(weight[i] > perfectWeight)
                System.out.println("Person should lose: " + (weight[i] - perfectWeight));
            else
                System.out.println("Person should gain: " + (perfectWeight - weight[i]));
        }

    }
}

First it calculate the perfect weight and then it is compared to Person weight and print if the person should lose or gain some kg

Java Program to calculate BMI or Body Mass Index, Java Program : static void print(String string) { System. if (bmi < 18.5) { result = "underweight"; println("Your BMI is " + bmi + " and that means " + result); } float bmi = (weight * 703) / (height * height); //print the result. public static void main(String[] args) { Scanner sc = new Scanner(System. if (userInput Input weight in kilogram: 55 Input height in meters: 1.5 The Body Mass Index (BMI) is 24.444444444444443 kg/m2. Now let us understand the above program. The values of the weight and height are obtained from the user in kilogram and meters respectively. The code snippet that demonstrates this is given as follows −.

I'd suggest you to write a class, let's call it Person. In each instance of this class, you can store the name, the height and weight of a given person.

class Person {
    private String name;
    private String familyName;
    private int weight;
    private double height;
}

In this class, it's really simple, to write a method which returns the BMI. To create these instances, you can use a method similar to the answer above, however, I'd recommend you do addiditonal checks, whether the arrays are of the same size.

Java Program to Calculate BMI (Body Mass Index), BMI or Body Mass Index is body mass measurement of an individual based on their height and weight. Using BMI a person can be classified into  2) Advance Java Program to Calculate BMI of an Individual and print if the individual is underweight, normal, overweight or obese according to following BMI categories. Less than 15 = Very severely underweight. bmi >= 15 but bmi < 16 = Severely underweight. bmi >= 16 but bmi < 18.5 = Underweight. bmi >= 18.5 but bmi < 25 = Normal (healthy weight)

Java Program to Calculate BMI (Body Mass Index) – Know Program, Develop Java program for a BMI calculator. The BMI stands for Body Mass Index. It is a measure of health based on height and weight. Learn Java by Examples: How to calculate BMI of body in Java ?Learn Java by examples. Everything you want to know about Java. Tutorials, Source Codes, SCJP, SCWCD and Ebooks.

How to calculate BMI of body in Java ?, nextDouble(); double weightInKilogram = weight * KILOGRAMS_PER_POUND; double heightInMeters = height * METERS_PER_INCH; double bmi  As Height Weight is stored in the a variable Weight and then used to calculate weight in Kgs. Weight in KG is then stored as BMIWEIGHT. System.out.println(“Enter Weight In Pounds”); Weight = ConsoleIo.readInteger(); // pounds to KG convertion double BMIWEIGHT = Weight * 0.453; BMIWEIGHT & BMIHEIGHT are then used to calculate the users BMI.

BMI Calculator in Java, Body Mass Index (BMI) is a measure of health based on height and weight. It can be calculated by taking your weight in kilograms and dividing it  Java program to calculate BMI ( Body Mass Index ): BMI or body mass index can be calculated by taking the weight in pounds and height in inches or by taking the weight in kilograms and height in meters.

BMI Calculator Using Arrays, package array;. import newport.advp.*; public class BMI { public static void main(​String[] args)throws Exception{ int Height, Weight; float tempB  Body Mass Index (BMI) is a measure of health based on height and weight. It can be calculated by taking your weight in kilograms and dividing it by the square of your height in meters. Write a java code to let the user enter weight, feet, and inches and interpret the users BMI.

Comments
  • First of all userBMI = weight[i]/height[j] * height[j]; is not a correct way to calculate BMI
  • What exactly should do method calculateBMI because it says that it should calculate BMI for each user but you pass name to this method ?
  • You should study Classes and Objects from the Java Lessons. You should not put all these data into a bunch of arrays, but use a class like Person instead. See László Stahorszki's answer. It looks like the calculateWeight method is defined by the assignment, but it's bad design.