0

Basically I need to write an application to calculate BMI using the formula;

BMI = w/ (h/100) 2

The user must type in her height in centimeters and weight in kilograms, and the computer prints out the user's BMI.

import java.util.Scanner;
import java.text.DecimalFormat;

public class BMI {

public static void main(String args[ ]){


double pound = .453592;
double inches = 2.54;


Scanner scan = new Scanner(System.in);

System.out.println("Enter in your weight: ");
double weight = scan.nextDouble();

System.out.println("Enter in your height: " );
double height = scan.nextDouble();

weight = weight * pound ;// Converts pounds to kilograms
height = height * inches ;// Converts inches to centimeters


double BMI = weight/(height*height);// Computes users BMI


DecimalFormat fmt = new DecimalFormat("0.00");// Rounds answer to 2 decimal places.

System.out.println("Your Body Mass Index is: " + fmt.format(BMI));

I keep getting "Your BMI is: 0". What am I doing wrong? Any suggestions, thanks in advance.

Also I'll be using JOptionPane.showInputDialog() to get user input and JOptionPane.showMessageDialog() to print the results to the user.

Edited by pritaeas: Closed

5
Contributors
7
Replies
21
Views
8 Years
Discussion Span
Last Post by JamesCherrill
0
import javax.swing.*;
import java.text.DecimalFormat; 
public class BMI {  	

public static void main(String args[ ]) 	{  	
     double Weight = .453592;	
     double Height = 2.54;  	

     weight = Double.parseDouble(JOptionPane.showInputDialog(null,"enter your weight in kilograms"));
     height = Double.parseDouble(JOptionPane.showInputDialog(null,"enter your height in kilograms"));
     double BMI = weight/(height*height);
     DecimalFormat fmt = new DecimalFormat("#.##");
     System.out.println("Your Body Mass Index is: " + fmt.format(BMI));
0
import javax.swing.*;
//import java.text.DecimalFormat; 

public class Main {  	

public static void main(String args[ ]) 	{  	
    int weight = (int) .453592; // converts to kg	
    int height = (int) 2.54; // converts to centimeters  	

     weight = (int)Double.parseDouble(JOptionPane.showInputDialog(null,"Enter your weight in kilograms"));
     
     height = (int)Double.parseDouble(JOptionPane.showInputDialog(null,"Enter your height in centimeters"));
     
     double BMI = weight/(height*height);
     
    // DecimalFormat fmt = new DecimalFormat("#.##");
     
     JOptionPane.showMessageDialog(null, "Your Body Mass Index is: " + BMI + ".");
}

}

61.23497 kg = weight
165.1 cm = height

Made a couple of changes...

So after getting rid of the "DecimalFormat" from the program( the output was way off), I was finally able to get the BMI output somewhat accurate; 0.0022464. It should actually read 22.464.

0

I switched the weight and height to integers(Int) and now it won't calculate the users BMI properly.

What am I doing wrong?

Everything looks about right, not sure why it doesn't work. Any help, much appreciated.

0


61.23497 kg = weight
165.1 cm = height

Made a couple of changes...

So after getting rid of the "DecimalFormat" from the program( the output was way off), I was finally able to get the BMI output somewhat accurate; 0.0022464. It should actually read 22.464.

Sorry I found the problem. The formula for Computing BMI using centimeters is; w
BMI = ___
(h/100) 2

Question; How do I add an exponent in java?? Or how do I add an exponent to the end of ; BMI = weight/(height/100)

double BMI = weight/(height/100);

Thanks in advance.

0

u can use the Math.pow() method...
OR
Multiply with itself... like this...
double BMI = weight/(height/100);
BMI=BMI*BMI;

Hope you've got the point...

Edited by anupam_smart: n/a

0

English BMI Formula
BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703
Metric BMI Formula
BMI = ( Weight in Kilograms / ( Height in Meters x Height in Meters ) )

These are the standerd BMI Formulas.
Both of them are correct and results the same.