1

I am new to Java and trying to make a basic body mass calculator. My problem is I need to ask the questions, convert the measurements and then pass it to another method then display the results in a separate method. I've added my code below but keep getting a 1.0 returned as the answer each time.

import java.util.Scanner;

public class calcBMI {

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner( System.in );

        System.out.print("Enter weight in pounds: ");
        double weightInPounds = keyboard.nextDouble();
        double weightInKg = (weightInPounds / 2.2);

        System.out.print("Enter height in inches: ");
        double heightInInches = keyboard.nextDouble();
        double heightInMeters = (heightInInches / 0.254);
        double resultBMI = 1;

        displayResults(resultBMI);
    }

    public static double bodyMassIndex(double weightInKg, double 
    heightInMeters)
    {
        double resultBMI = weightInKg / Math.pow(heightInMeters, 2) * 1.375;

        return resultBMI;
    }        

    public static void displayResults(double resultBMI)
    {

        System.out.printf("The calculated body mass index was: " + resultBMI);

        System.out.println();
    }
}

Updated code, now getting; Enter weight in pounds: 180 Enter height in inches: 68 The calculated body mass index was: 1.1415618118905313E-5 BUILD SUCCESSFUL (total time: 3 seconds)

import java.util.Scanner;

public class calcBMI {

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner( System.in );

        System.out.print("Enter weight in pounds: ");
        double weightInPounds = keyboard.nextDouble();
        double weightInKg = (weightInPounds / 2.2);

        System.out.print("Enter height in inches: ");
        double heightInInches = keyboard.nextDouble();
        double heightInMeters = (heightInInches / 0.0254);


        displayResults(bodyMassIndex(weightInKg, heightInMeters));
    }

public static double bodyMassIndex(double weightInKg, double heightInMeters)
    { 
        return (weightInKg / Math.pow(heightInMeters, 2));
    }        

public static void displayResults(double resultBMI)
    {

        System.out.printf("The calculated body mass index was: " + resultBMI);

        System.out.println();
    }
}
  • I also need to round the BMI result to 1 decimal place using the printf method... I've tried adding the code System.out.printf("The calculated body mass index was: %.1f" + resultBMI); but I get an error when doing so –  Jan 31 '16 at 20:44
  • Correct your calculation for height first. Then test the weight and height with `160 lb` and `70 inch`. You should get `BMI 23`. It will work. – user3437460 Jan 31 '16 at 20:46
  • Holy shit batman it works. Got any idea on how I can make that rounded to one decimal place? –  Jan 31 '16 at 20:50
  • Take a look here.. http://stackoverflow.com/questions/22186778/using-math-round-to-round-to-one-decimal-place – user3437460 Jan 31 '16 at 20:52
  • Haha I figured it out before I seen your link... System.out.printf("The calculated body mass index was " + "%.1f", + resultBMI); –  Jan 31 '16 at 21:01
  • @user3437460 - one more quick question if I need to have the displayResults method pull info from another method, how would I get main to call this? i'll add code bits if needed. –  Jan 31 '16 at 21:17
  • Never mind, I figured it out, thanks all for the assistance =D –  Jan 31 '16 at 21:33

2 Answers2

1

You are not calling the bodyMassIndex method in your code at all. Change

displayResults(resultBMI);

to

displayResults(bodyMassIndex(weightInKg, heightInMeters));

resultBMI equals 1, so of course the output would always be :

"The calculated body mass index was: 1"

Full code:

public static void main(String[] args) {

    System.out.print("Enter weight in pounds: ");
    double weightInPounds = keyboard.nextDouble();
    double weightInKg = (weightInPounds / 2.2);

    System.out.print("Enter height in inches: ");
    double heightInInches = keyboard.nextDouble();
    double heightInMeters = (heightInInches / 0.254);        

    // You can get rid of the resultBMI variable
    displayResults(bodyMassIndex(weightInKg, heightInMeters));
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • you might want to make those float literals double literals... for precision and to prevent implicit casting, like this: `2.2d` and `0.254d` – randers Jan 31 '16 at 19:37
  • 1
    Those already are double literals. For floating point numbers, double is the default in Java. – Tesseract Jan 31 '16 at 19:44
  • I made the changes however now I'm getting a value returned of; "The calculated body mass index was: 0.0011415618118905313," I should be getting somewhere between 10 and 30, as 18.5 to 24.9 is normal –  Jan 31 '16 at 20:20
  • The output depends on your input. What do you provide as input to the program ? – Mohammed Aouf Zouag Jan 31 '16 at 20:21
  • 180 weight, and 68 inches, I just type them in as 180, then 68 –  Jan 31 '16 at 20:23
0

You get 1.0 because you hard coded it as such.

Change this:

double resultBMI = 1;

To:

double resultBMI = bodyMassIndex(weightInKG, heightInMeters);

By the way, you could also have returned BMI directly in the method and there is no need to multiply by 1.375 anymore since you are already supplying weight in KG:

public static double bodyMassIndex(double weightInKg, double heightInMeters)
{
    return (weightInKg / (heightInMeters*heightInMeters));
} 

Add on:

Your conversion from inches to meters is wrong as well. It should be:

double heightInMeters = (heightInInches * 0.0254);
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • I made the changes however now I'm getting a value returned of; The calculated body mass index was: 0.0011415618118905313, I should be getting somewhere between 10 and 30, as 18.5 to 24.9 is normal –  Jan 31 '16 at 20:16
  • @BradHillis That is because your conversion is wrong. 1 inch = 0.0254 m, not 0.254. – user3437460 Jan 31 '16 at 20:23
  • Made the correction now getting, The calculated body mass index was: 1.1415618118905313E-5 –  Jan 31 '16 at 20:26
  • @BradHillis I just updated my solution. Did you calculate it as I did? – user3437460 Jan 31 '16 at 20:27
  • @BradHillis Like I said, you didn't calculate heightInMeters correctly. It should be `(heightIntInches * 0.0254)`. You are dividing it. – user3437460 Jan 31 '16 at 20:45