Java – if (Float.isNaN(x))

if (Float.isNaN(x))… here is a solution to the problem.

if (Float.isNaN(x))

float aa = Float.valueOf(a.getText().toString());
if (Float.isNaN(aa)) {
    aa = 0;

I’m trying to check if the user has no input and set it to zero if there is no input, which sounds simple but doesn’t work for me. This is my first android app and it crashes when there is no user input and the user presses the go button. Who can help?

Thank you.


I’m trying to check for no input from the user and if there’s no input just make it zero.

When there is no input or the input is invalid, valueOf does not return NaN; it throws You can add a try/catch around the valueOf call (because you need float, not float, you should use it). parseFloat to avoid unnecessary boxing and unboxing of values:

float aa;

try {
    aa = Float.parseFloat(a.getText().toString());
} catch (NumberFormatException nfe) {
    aa = 0;

Related Problems and Solutions