I created a java program to calculate a persons BMI, but we have to convert the weight from pounds to kilos and height from inches to meters using the numbers in my program, but it just doesnt worl the way its suppose to can some PLEASE help me figure it out.


//Body Mass Index.java
//Davina Moore
//Due: September 18, 2008
//This java program is designed to compute your Body Mass Index


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

public class Body_Mass_Index
{

public static void main(String args[ ])
{
double pound = .0453592;
double inches = .0254;

double weight = pound *.0453592;
double height = inches *.0254;
double BMI = weight/(height*height);

Scanner scan = new Scanner(System.in);

System.out.println("Please enter in your weight: ");
weight = scan.nextDouble();

System.out.println("Please enter in your height: ");
height = scan.nextDouble();

//rounds the answer to 2 decimal places
DecimalFormat fmt = new DecimalFormat("0.##");

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

}

}

Hello,

try this :

double pound = .0453592;
double inches = .0254;

Scanner scan = new Scanner(System.in);

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

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

weight *= pound;
height *= inches;
double BMI = weight/(height*height);

//rounds the answer to 2 decimal places
DecimalFormat fmt = new DecimalFormat("0.##");

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

If you cant understand the changes i made and most important why i made these changes. Please post here :) I will be glad to help you.

Regards,
Puneet

i still dont get the right answer
why you make the changes you did

i still dont get the right answer
why you make the changes you did

Hello again,

There must be some calculation problem then because program is working and giving an output too. I dont know about BMI formula so i cant tell you surely.

weight *= pound; // Equals to weight = weight * pound and pound = .0453592; 
height *= inches; // Equals to height = height * inches and inches = .0254;
double BMI = weight/(height*height); // Formula for BMI Calculation.

We have initialized pound and inches variables at top before everything. That is good and well.

but you did all calculation before input values (weight & height) from console. That is wrong.

So i moved calculation part down after input from console.

Hope this will help you.

Regards,
PuneetK

What puneetkay is trying to say is that you must first read the input from the console and then do the calculations, not the other way around

What puneetkay is trying to say is that you must first read the input from the console and then do the calculations, not the other way around

No, Im trying to help him with the simplest way as you can see he is newbie.

Even, im a newbie. im just sharing my knowledge here :) If im wrong, you seniors are here to help us.

Regards,
PuneetK

No, Im trying to help him with the simplest way as you can see he is newbie.

Even, im a newbie. im just sharing my knowledge here :) If im wrong, you seniors are here to help us.

Regards,
PuneetK

No, you fixed some of his code and then provided no explanation of what you did or why, which does nothing to promote learning. javaAddict was merely trying to provide the explanation that you did not bother to give.

thanks for everyone's help and input.....
my program works fine.

Just a side note i'm not a newbie its just im new to this site, and i had a very bad java teacher in the past so now i have to retake java to get into grad school....

fyi, im a her not a he...
lol

thanks for everyone's help and input.....
my program works fine.

Just a side note i'm not a newbie its just im new to this site, and i had a very bad java teacher in the past so now i have to retake java to get into grad school....

fyi, im a her not a he...
lol

Oops! sorry about that. Please mark this thread as Solved. :P

Regards,

I created a java program to calculate a persons BMI, but we have to convert the weight from pounds to kilos and height from inches to meters using the numbers in my program, but it just doesnt worl the way its suppose to can some PLEASE help me figure it out.

//Body Mass Index.java
//Davina Moore
//Due: September 18, 2008
//This java program is designed to compute your Body Mass Index

import java.util.Scanner;

public class CalculateBMI {

    public static void main(String[] args) {
            double weight;
    double foot=0.3048;
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter your weight: ");
    weight = scan.nextDouble();
    System.out.println("Please enter your height: ");
    double height = scan.nextDouble();
    height= height*foot;
    double BMI = weight/(height*height);
     System.out.println("Your Body Mass Index is: " +BMI);

}
commented: Well over a year late, no code tags, nothing new to add anyway -4

No, you've just dragged up a year-old question that has already beed resolved and posted some code with no explanation. Do you think that is helpful?

well, the original was also wrong.
The politically correct solution is simple:

System.out.println("you're morbidly obese");

is the correct response to any data entered into the application.

You never converted weight in pounds to kilograms' which is why the program gave the wrong answer

this is how i changes it:

import java.util.Scanner;


public class CalculateBMI 
{

    public static void main(String[] args) 
    {
        double weight;
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter your weight in pounds: ");
        weight = scan.nextDouble();
        double weightkg = weight/2.2;
        System.out.println("Please enter your height in inches: ");
        double heightIn = scan.nextDouble();
        double heightM = heightIn*(.305/12);
        double BMI = weightkg/(heightM*heightM);
        System.out.println("Your Body Mass Index is: " +BMI);
    }

}
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.