Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumberFormatException Error

Tags:

java

android

the code is:

editText2=(EditText) findViewById(R.id.editText2);
editText3=(EditText) findViewById(R.id.editText3);
float from_value= Float.parseFloat(editText2.getText().toString());
editText3.setText(" "+(from_value * 100.0));

And the logcat error is:

03-18 03:19:07.847: E/AndroidRuntime(875): Caused by: java.lang.NumberFormatException: Invalid float: ""

like image 577
Exorcist Avatar asked Dec 13 '22 04:12

Exorcist


1 Answers

Seems like the String in editText2 is empty, so it fails to parse it as float.

A possible solution is to check if the String is empty first, and then decide about default value, another is to catch the exception:

float from_value;
try {
    from_value = Float.parseFloat(editText2.getText().toString());
}
catch(NumberFormatException ex) {
    from_value = 0.0; // default ??
}
like image 92
MByD Avatar answered Dec 27 '22 06:12

MByD