-1

What I want to do :

I made a BMI calculator (Formula=(KG*KG)/M) The input takes eg 186 but i get an error when inserting 1.86 (For height in metres)

java.lang.NumberFormatException: For input string: "1.86"

Here is my code :

    if (txtHeight.getText().length() > 0 && txtWeight.getText().length() > 0){
        height = Integer.parseInt(txtHeight.getText());
        heightsqr = (int) Math.pow(height, 2);
        mass = Integer.parseInt(txtWeight.getText());
        bmi = (heightsqr/ mass);
        lblBmi.setText("Current BMI : " + Integer.toString(bmi));
        }
        else{
            JOptionPane.showMessageDialog(this, "Please enter your weight in KG and your Height in M");
        }

It's a pretty basic error that I just cant seem to fix

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 3
    Possible duplicate of [What is a NumberFormatException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – xenteros Nov 14 '16 at 08:39
  • use double instead of int – Jobin Nov 14 '16 at 08:39
  • I think you need a basic understanding about datatypes in java. Look what an Integer could store and what alternatives you have. – smsnheck Nov 14 '16 at 08:40
  • Instead of `Integer.parseInt` use `Double.parseDouble` you have the decimal values in `string` not the integers. – Umais Gillani Nov 14 '16 at 08:40
  • Did my answer help you? If you need some clarification, please ask in a comment. – xenteros Nov 14 '16 at 10:16

3 Answers3

1

As the stacktrace says, you can't convert 1.86 to int. Why? because it's a double.

try:

mass = Double.parseDouble(txtWeight.getText());

In Java, we have types. int can only store integer. Real numbers can be stored in floats and doubles but remember, that you can lose precision.

I don't know your whole code, so I can't tell you, what is the type of mass. In case you have the following somewhere in your code:

int mass;

change it to:

double mass;

Some random thoughts:
As you have already seen, there is a possibility that the Exception will be thrown. It happens depending on user input I believe. In Java we have a mechanism to secure. It's a try-catch block. It can be used for example in the following way:

try {
    mass = Double.parseDouble("1,86");
} catch (NumberFormatException e) {
    //somehow notify user that he has mistaken and work it out according to your business logic.
}

I strongly recommend reading this tutorial which I have provided on StackOverflow question What is a NumberFormatException and how can I fix it.

Community
  • 1
  • 1
xenteros
  • 15,586
  • 12
  • 56
  • 91
0

an Integer is a non-decimal value, hence 1.86 is invalid.

Try converting it into a Double: Double.parseDouble(txtHeight.getText());

xenteros
  • 15,586
  • 12
  • 56
  • 91
Roel Strolenberg
  • 2,922
  • 1
  • 15
  • 29
-1

you try to convert a double into an int. thats causing the error. if you want to fix it, you should either only accept integers or only doubles. otherwise you will need 2 if-statements. converting to double looks like this: height = Double.parseDouble(txtHeight.getText());

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65