Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need Help with Operators/ logic

1. ## Need Help with Operators/ logic

Okay, I'm very, very new to java. I'm in an intro to computer science class and we got out first assignment to write a program. its from "An Into to Object Oriented Programming with java" by C. Thomas Wu.

all we have to do is create a program that calculate body mass index using the formula Bmi=Weight/(Height/100)^2 where weight is in kg and height is in cm.

Here is my code:
```
import javax.swing.*;

class Bmi
{
public static void main(String[] args)
{
//*****
// Declarations:
//*****

final double NUM1=100.0;	//The constant 100 in the formula
String weightin;				//weight input
String heightin;				//height input
String description;			//if the output bmi is normal
int weightp;					//weight primitive
int heightp;					//height primitive
double denom;					//height/NUM1
double denommod;				//(height/NUM1)(height/NUM1)
double bmi;						//final value-bmi

heightin=JOptionPane.showInputDialog(null,"Please Enter your weight:\n (Kilograms)"); 						 //get weight
weightin=JOptionPane.showInputDialog(null,"Thanks you. Now, Please enter your Height:\n (centimeters)"); //get height

weightp=Integer.parseInt(weightin);		// convert height and weight to primitive values
heightp=Integer.parseInt(heightin);

denom=(heightp/NUM1);			//calulations
denommod=(denom*denom);
bmi=(weightp/denommod);

if(bmi<=18.5);							//Understanding what the value means
description="underweight";
if((bmi>18.5)&(bmi<=25));
description="normal";
if((bmi>25)&(bmi<=30));
description="overweight";
if(bmi>30);
description="obese";

JOptionPane.showMessageDialog(null,"Your Body mass index is:" +bmi+ " \n This is considered " +description+".");
}
}```

I think the problem is in the middle chunk there with the operators labelled "calculations"
but it could be my data types?
my professor (who i think to be completely incompetent) told us to use these test parameters to start:
50 kg and 160 cm

i keep getting 640 with this program but the correct answer is roughly 19.5

if you could point out any convention errors, too, that would be great!
thanks!  Reply With Quote

3. ## Re: Need Help with Operators/ logic

I don't notice anything wrong. Your formula seems correct and there is nothing noticeably wrong syntactically.  Reply With Quote

4. ## Re: Need Help with Operators/ logic

Try debugging your code by printing out the values of all the variables used in your calculations to see where your formulas are going wrong.  Reply With Quote

5. ## Re: Need Help with Operators/ logic

Mahaa! i found it. if you look at the code, i had prompted for weight and stored it in height and vice versa.

but now theres another problem!

heres the new code:
```import javax.swing.*;

class Bmi
{
public static void main(String[] args)
{
//*****
// Declarations:
//*****

final double NUM1=100.0;	//The constant 100 in the formula
String weightin;				//weight input
String heightin;				//height input
String description;			//if the output bmi is normal
int weightp;					//weight primitive
int heightp;					//height primitive
double denom;					//height/NUM1
double denommod;				//(height/NUM1)(height/NUM1)
double bmi;						//final value-bmi

heightin=JOptionPane.showInputDialog(null,"Please Enter your Height:\n (Centimeters)"); 						 //get weight
weightin=JOptionPane.showInputDialog(null,"Thanks you. Now, Please enter your Weight:\n (Kilograms)"); //get height

weightp=Integer.parseInt(weightin);		// convert height and weight to primitive values
heightp=Integer.parseInt(heightin);

denom=(heightp/NUM1);			//calulations
denommod=(denom*denom);
bmi=(weightp/denommod);

if(bmi<=18.5);							//Understanding what the value means
description="underweight";
if((bmi>18.5)&(bmi<=25));
description="normal";
if((bmi>25)&(bmi<=30));
description="overweight";
if(bmi>30);
description="obese";

JOptionPane.showMessageDialog(null,"Your Body mass index is:" +bmi+ " \n This is considered " +description+".");
}
}```

This time the problem is that it always tells me the bmi is obese. obviously an issue with the boolean logic. this isnt required for the project but i wanted to experiment. also, how do i keep the program from delivering the bmi with more than just a few decimal places?
Thanks!  Reply With Quote

6. ## Re: Need Help with Operators/ logic

how do i keep the program from delivering the bmi with more than just a few decimal places
Look at the DecimalFormat class for formatting doubles to desired formats.  Reply With Quote

7. ## Re: Need Help with Operators/ logic

okay, all fixed. thanks for the help!  Reply With Quote