# Body Mass Index Calculator

• 11-01-2010, 03:24 AM
bentho
Body Mass Index Calculator
I am working on a bmi calculator that is a revision of an example program in my textbook. The program lets user enter weight (in pounds) and height (in feet and inches) and then calculates bmi. Weight is converted to kilograms and height is converted to meters.

I have this so far:
Code:

```import java.util.*; public class ExerciseSix {     //Attributes     private double weight, feet, inches;     final double kgPerPound = 0.45359237;     final double metPerInch = 0.0254;     //Constructors     public ExerciseSix()     {         Scanner reader = new Scanner(System.in);         System.out.println("Enter weight in pounds: ");         weight = reader.nextDouble();         System.out.println("Enter height in feet and inches: ");         feet = reader.nextDouble();         inches = reader.nextDouble();     }     //Methods     public void bodyMassIndex()     {         double wtInKilos = weight * kgPerPound;         double htInInches = ((feet / 12) + inches);         double htInMeters = htInInches * metPerInch;         double bmi = wtInKilos / (htInMeters * htInMeters);         System.out.printf("Your BMI is %5.2f\n" , bmi);     } }```

Only difference from example in the book is they used height in inches from start, everything else is same. I am getting large numbers like 946 for bmi when it should be like 20.95. Problem may be in conversion from feet to inches and then inches to meters. Anybody see what I am missing.
• 11-01-2010, 03:37 AM
Fubarable
Quote:

Originally Posted by bentho
Code:

`double htInInches = ((feet / 12) + inches);`

How do you calculate the number of inches in each foot? Is it feet / 12, or is it perhaps something else? ;)

Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Code:

```[cod&#101;]   // your code goes here   // notice how the top and bottom tags are different [/cod&#101;]```
I've added code tags to your post above so you should be able to see the improvement for yourself.

Best of luck!
• 11-01-2010, 03:55 AM
bentho
I figured it out. Not sure why I was dividing feet by 12 instead of multiplying by 12 guess it was a brain fart. I didn't know that about the code tags and formatting.

Thanks
• 11-01-2010, 04:05 AM
Fubarable
Quote:

Originally Posted by bentho
I figured it out. Not sure why I was dividing feet by 12 instead of multiplying by 12 guess it was a brain fart.

Sometimes it's hard to see when it's buried deep in your code. The way that I solved it was to place println statements to check the values of variables to see if they made sense. For example:

Code:

```  public void bodyMassIndex() {       double wtInKilos = weight * kgPerPound;       System.out.println("wtInKilos: " + wtInKilos);       double htInInches = ((feet / 12) + inches);       System.out.println("htInInches: " + htInInches); // *** this result didn't make sense       double htInMeters = htInInches * metPerInch;       double bmi = wtInKilos / (htInMeters * htInMeters);       System.out.printf("Your BMI is %5.2f\n", bmi);   }```

Quote:

I didn't know that about the code tags and formatting.
No problem. You do now. :)

Quote:

Thanks
You're welcome!