# Thread: Body Mass Index Calculator

1. Member
Join Date
Oct 2010
Posts
13
Rep Power
0

## 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:
Java 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()
{
System.out.println("Enter weight in pounds: ");
System.out.println("Enter height in feet and inches: ");
}
//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.
Last edited by Fubarable; 11-01-2010 at 02:33 AM. Reason: moderator edit: code tags added

2. Originally Posted by bentho
Java 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:

Java Code:
```[cod&#101;]
// 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!
Last edited by Fubarable; 11-01-2010 at 02:48 AM.

3. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
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

4. 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:

Java 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);
}```

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

Thanks
You're welcome!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•