0

I've written a code for Body Mass Index which takes the weight & height in Pounds - Inches and converts to Kg - Meters and then calculates BMI.

The problem is whenever I try to run the program an error appears says:

"Exception in thread "main" java.util.InputMismatchException" 

and these following lines:

at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at BMI.main(BMI.java:12)"

I've been trying to solve it for 2 hours but no dice, I don't know what is needed to be changed exactly.

Here's my code

import java.util.Scanner;
public class BMI {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    final double KgPerPound= 0.45359237;
    final double MetersPerInch= 0.0254;
    
    Scanner input= new Scanner(System.in);
    
    System.out.println("Enter weight in Pounds: ");
    double Weight = input.nextDouble();
    
    System.out.println("Enter Height in Inches: ");
    double Height = input.nextDouble();
    
    double P_to_KG = (Weight * KgPerPound);
    double I_to_M = (Height * MetersPerInch);
    double BMI = (P_to_KG / Math.pow(I_to_M, 2));
    
    System.out.println("BMI is = " + BMI );
  }

}

I'm completely new to programming so I'm sorry for my newbie question :)

  • 3
    Does this answer your question? [Why am I getting InputMismatchException?](https://stackoverflow.com/questions/14027537/why-am-i-getting-inputmismatchexception) – flaxel Sep 28 '20 at 21:21
  • Your code seems to be working fine providing that valid data are available in the standard input stream `System.in` – Nowhere Man Sep 28 '20 at 21:23
  • @flaxel Yes thank you very much! it appears that I was entering an invalid value. – Bin Duhaim Sep 28 '20 at 22:41
  • @AlexRudenko Yep sorry for the confusion, it was a silly error and now I'm embarrassed haha. – Bin Duhaim Sep 28 '20 at 22:45

0 Answers0