1,105,380 Community Members

BMI calculation

Member Avatar
dantheman4
Newbie Poster
19 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi I am just starting to learn about java.I am trying to calculate BMI of an individual using the JOptionPane.showInputDialog method who I am asking to enter 4 things first name,last name,height in inches and weight in pounds. I want to show the BMI using JOptionPane.showMessageDialog method showing their full name and BMI.What am I doing wrong ?

import javax.swing.JOptionPane;

public class P1 {

    public static void main(String[] args) {

        String FirstName = JOptionPane.showInputDialog("Enter your First name");
        String LastName = JOptionPane.showInputDialog("Enter your Last name");
        double height = JOptionPane.showInputDialog("Enter your height in inches");
        int inches = Integer.parseInt(height);
        int metre= inches*0.0254;

        double weight =JOptionPane.showInputDialog("Enter your weight in pounds");
        int pounds = Integer.parseInt(weight);
        int kilo= pounds/2.2;

        String response=
                "kilo"*sqrt"metre" +"FirstName"+"Lastname"

        JOptionPane.showMessageDialog(null, response);
    }

}
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

I want to show the BMI using JOptionPane.showMessageDialog method showing their full name and BMI.What am I doing wrong ?

Can you explain what the program does that is "wrong"?

Member Avatar
rubberman
Senior Poster
3,999 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
0
 

Try this:

        double results = kilo * sqrt(metre);
        String response= results + " " + FirstName + " " + Lastname;
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 
Member Avatar
dantheman4
Newbie Poster
19 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi by wrong I mean this is the error when i try to run my java application
Type mismatch: cannot convert from String to double

The method parseInt(String) in the type Integer is not applicable for the arguments
(double)
Type mismatch: cannot convert from double to int
The method sqrt(int) is undefined for the type P1
at P1.main
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

The posted error messages left off the source code line numbers.

parseInt(String) in the type Integer is not applicable for the arguments (double)

The parseInt() method requires a String for its arg, not a double. What data are you trying to convert to an int?

The method sqrt(int) is undefined

The compiler can not find a definition for the method: sqrt()? What class is it defined in?
Where is it defined?

Type mismatch: cannot convert from double to int

What line did this error happen on?

Member Avatar
dantheman4
Newbie Poster
19 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Type mismatch: cannot convert from double to int
this happened on line 11 and line 15

The method sqrt(int) is undefined
I fixed that using Math.sqrt

cannot convert from string to double
This happened on line 9

parseInt(String) in the type Integer is not applicable for the arguments (double)
I am trying to convert height and weight into an int for my BMI

Member Avatar
dantheman4
Newbie Poster
19 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have got most of my coding sorted.But i still cant figure out how to make the user enter their height and weight without 0 and 0 because if i dont put eg double inches=0; then it says local variable is not initialized I want the user to enter any number for weight and height and still calculate.

import javax.swing.JOptionPane;



public class P1 {

    public static void main(String[] args) {

        double inches=0;
        double pounds=0;

        String FirstName = JOptionPane.showInputDialog("Enter your First name");
        String LastName = JOptionPane.showInputDialog("Enter your Last name");

        String height = JOptionPane.showInputDialog("Enter your height in inches");
        double metre= inches*0.0254;

        String weight =JOptionPane.showInputDialog("Enter your weight in pounds");
        double kilo= pounds/2.2;

        double results = kilo * Math.sqrt(metre);
        String response= results + " " + FirstName + " " + LastName;

        JOptionPane.showMessageDialog(null, response);
    }

}
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

What do you do with the height and weight values that the user enters in response to the JOptionPane prompts? Should the String that the user entered be converted to a numeric value so you can do arithmetic with it to compute the values you want to display?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: