-1

I'm pretty new to programming and am currently teaching myself Java. I'm trying to make a program that involves BMI, but I keep on having 0 displayed instead of a BMI value.

I'm pretty sure my error is in the math, but I don't know what to change:

public class bmi {
    public static void main(String[] args) {
        Scanner k = new Scanner(System.in);

        System.out.print("Weight(kg):");
        int weight=k.nextInt();

        //now for the second scanner or int
        Scanner j = new Scanner(System.in);
        System.out.print("Height (m):");

        int height=j.nextInt();
        double bodyMassIndex = ((double) weight / (height * height));

        //so far this looks fine i believe , however there might be a problem with the math. 
        System.out.println(bodyMassIndex);
    }
}
Momo
  • 3,542
  • 4
  • 21
  • 34
imp
  • 1
  • 2

1 Answers1

1

Change to double bodyMassIndex = (703 * ((double) weight / (height * height)));

EDIT:

public class bmi 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in); // Only need one scanner for multiple variables

        System.out.print("Weight(kg):");
        double weight = input.nextDouble();

        System.out.print("Height (m):");
        double height = input.nextDouble();

        double bodyMassIndex = (weight / (height * height));

        System.out.println(bodyMassIndex);
    }
}
Riley Carney
  • 804
  • 5
  • 18
  • Thank you. Could you please explain to me why I'm getting a "Exception in thread 'main' java.util.InputMismatchExcpetion" error? (see the code i edited into the original post) – imp Dec 02 '15 at 00:16
  • I would post code like I said in my edited post. – Riley Carney Dec 02 '15 at 19:02