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

}

Dani AI

Generated

Short summary and a working pattern to fix the type/logic problems you hit. The JOptionPane methods always return a String; convert those Strings to numeric types with the appropriate parser (use Double.parseDouble for values that may have decimals). Keep your height and weight as double, convert units explicitly, and compute BMI with the standard formula: weight_kg / (height_m * height_m). was seeing type-mismatch errors because numeric parsing and variable assignments were not done in the right order. suggested a sqrt approach, but BMI requires division by height squared, not a square root.

Here is a minimal, self-contained example showing parsing, unit conversion and formatted output:

import javax.swing.JOptionPane;

public class BMIExample {
    public static void main(String[] args) {
        try {
            String first = JOptionPane.showInputDialog("First name:");
            String last  = JOptionPane.showInputDialog("Last name:");
            String sIn   = JOptionPane.showInputDialog("Height (inches):");
            String sLb   = JOptionPane.showInputDialog("Weight (pounds):");
            if (sIn == null || sLb == null) return; // user cancelled

            double inches = Double.parseDouble(sIn);
            double pounds = Double.parseDouble(sLb);

            double meters = inches * 0.0254;
            double kilos  = pounds / 2.20462;
            double bmi    = kilos / (meters * meters);

            String msg = first + " " + last + "  BMI: " + String.format("%.1f", bmi);
            JOptionPane.showMessageDialog(null, msg);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Please enter numeric values for height and weight.");
        }
    }
}

Quick tips: use Double.parseDouble when decimals are possible; avoid initializing a numeric variable to 0 and then never assigning the parsed value to it; check for null if the user cancels; and format the BMI for readability. 's request for the failing lines was the right move — always check the compiler line numbers to find which variable or parse is incorrect.

Recommended Answers

All 8 Replies

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"?

Try this:

        double results = kilo * sqrt(metre);
        String response= results + " " + FirstName + " " + Lastname;

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

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?

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

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

}

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.