5 Replies - 4298 Views - Last Post: 24 February 2010 - 04:57 PM Rate Topic: -----

#1 Guest_385Audio*


Reputation:

Having problems with coding

Posted 23 February 2010 - 09:08 PM

Write a program that calculates and displays a person's body mass index (BMI). The BMI is often used to determine whether a person with a sedentary lifestyle is overweight or under-weight for his or her height. A person's BMI is calculated with the following formula:
BMI = weight x 703 / height^2
Where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A sedentary person's weight is considered optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered underweight. If the BMI value is greater than 25, the person is considered overweight.

This is what I have but I keep getting errors when I compile it......


import javax.swing.JOptionPane; // Needed for JOptionPane Class
public class PC3
{
public static void main(String[]args)
{
String input;
double height;
double weight;
double bodyMassIndex = (weight * 703) /(height * height);


// Prompt the user for a height.
input = JOptionPane.showInputDialog("What is your weight ");

weight = Double.parseDouble(input);

// Prompt the user for a weight.
input = JOptionPane.showInputDialog("What is your height ");

height = Double.parseDouble(input);

// Determine whether the user is Optimal, Under, or Overweight.




System.out.println("BodyMassIndex ");



}
}

Is This A Good Question/Topic? 0

Replies To: Having problems with coding

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Having problems with coding

Posted 23 February 2010 - 09:19 PM

Stop you right here

double height;
double weight;
double bodyMassIndex = (weight * 703) /(height * height);

height and weight are not initialized (you haven't put values into them yet)
you can not use them in mathematical calculations

Welcome at DIC and please :code: even for short code snippets
Was This Post Helpful? 0
  • +
  • -

#3 385Audio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-February 10

Re: Having problems with coding

Posted 24 February 2010 - 04:34 PM

View Postpbl, on 23 February 2010 - 08:19 PM, said:

Stop you right here

double height;
double weight;
double bodyMassIndex = (weight * 703) /(height * height);

height and weight are not initialized (you haven't put values into them yet)
you can not use them in mathematical calculations

Welcome at DIC and please :code: even for short code snippets


Ok Thankyou! Now I got more code and I think I almost got it but it gives me one error on the = sign for the if statement! I can't figure out what i'm doing wrong on that... Here it is :

import javax.swing.JOptionPane;         // Needed for JOptionPane Class
public class PC3 
{ 
        public static void main(String[]args) 
        { 
	        	String input;                                            
                double height;
                double weight;   
                double bodyMassIndex = 100; 
                  
                
                // Prompt the user for a weight.
                input = JOptionPane.showInputDialog("What is your weight ");
                weight = Double.parseDouble(input);
                
                // Prompt the user for a height.
                input = JOptionPane.showInputDialog("What is your height ");
                height = Double.parseDouble(input);
                
                bodyMassIndex = (weight * 703)/(height * height);
                
                // Determine whether the user is Optimal, Under, or Overweight.
                if (BMI > = 18.5 && BMI < 25)
                {
	                JOptionPane.showMessageDialog(null, "You are Optimal weight " + "Your BMI is " + BMI);
                }
                else if (BMI < 18.5)
                {
	                JOptionPane.showMessageDialog(null, "You are underweight" + "Your BMI is " + BMI);
                }
                	else
                {
	                JOptionPane.showMessageDialog(null, "You are overweight " + "Your BMI is " + BMI);
                }
	                
	            System.out.println("BodyMassIndex ");
                
                
                
        } 
}


This post has been edited by 385Audio: 24 February 2010 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Having problems with coding

Posted 24 February 2010 - 04:41 PM

you have blank space between the ">" operator and the "=" operator, which is illegal.
when you combine those operators (> and =) to create the "bigger or equal to", they must not be seperated.
so this is illegal:
//wrong, seperated by space
if(BMI > = 18.5)


and this code is ok:
//ok, not seperated.
if(BMI >= 18.5)

Was This Post Helpful? 1
  • +
  • -

#5 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Having problems with coding

Posted 24 February 2010 - 04:42 PM

BMI is not declared, instead, use bodyMassIndex:

bodyMassIndex = (weight * 703)/(height * height);
                
                // Determine whether the user is Optimal, Under, or Overweight.
                if (bodyMassIndex >= 18.5 &&  bodyMassIndex < 25)


This post has been edited by redhotfire0: 24 February 2010 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 385Audio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-February 10

Re: Having problems with coding

Posted 24 February 2010 - 04:57 PM

View Postjapanir, on 24 February 2010 - 03:41 PM, said:

you have blank space between the ">" operator and the "=" operator, which is illegal.
when you combine those operators (> and =) to create the "bigger or equal to", they must not be seperated.
so this is illegal:
//wrong, seperated by space
if(BMI > = 18.5)


and this code is ok:
//ok, not seperated.
if(BMI >= 18.5)



Thankyou for the info! It worked better like that....

View Postredhotfire0, on 24 February 2010 - 03:42 PM, said:

BMI is not declared, instead, use bodyMassIndex:

bodyMassIndex = (weight * 703)/(height * height);
                
                // Determine whether the user is Optimal, Under, or Overweight.
                if (bodyMassIndex >= 18.5 &&  bodyMassIndex < 25)



Ok I see now what you're saying! I tried it out and it worked for me!! Thankyou....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1