We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,973 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

BMI calculation

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

}
3
Contributors
8
Replies
21 Hours
Discussion Span
8 Months Ago
Last Updated
11
Views
dantheman4
Newbie Poster
19 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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"?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Try this:

        double results = kilo * sqrt(metre);
        String response= results + " " + FirstName + " " + Lastname;
rubberman
Posting Maven
2,571 posts since Mar 2010
Reputation Points: 365
Solved Threads: 305
Skill Endorsements: 52
NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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
dantheman4
Newbie Poster
19 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

dantheman4
Newbie Poster
19 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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);
    }

}
dantheman4
Newbie Poster
19 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0791 seconds using 2.7MB