Search code examples
javaif-statementdouble

How do I compute these to make my BMI Calculator work?


    String A;
    String B;
    Double num1, num2;

    System.out.print("Enter your weight in kg: ");
    A = br.readLine();
    num1 = Double.parseDouble(A);
    System.out.print("Enter your height in cm: ");
    B = br.readLine();
    num2 = Double.parseDouble(B);

    **double bmi = (100 * 100 * A) / (B * B);**
    System.out.println("" + bmi);

    if (bmi < 18.5) {
        System.out.println("You are underweight");
    } else if (bmi < 25) {
        System.out.println("You are normal");
    } else if (bmi < 30) {
        System.out.println("You are overweight");
    } else {
        System.out.println("You are obese");
    }
}

I think the problem is in this line, how do i compute these so it works? TIA! :double bmi = (100 * 100 * A) / (B * B);


Solution

  • You're assigning the parsed values of A and B to num1 and num2 respectively. num1 & num2 now contain the parsed values while A and B still hold their original string values. For this code to work you'd have to write it like this: double bmi = (100 * 100 * num1) / (num2 * num2).