Mekillerow
Mekillerow

Reputation: 65

Add a value 0 to a variable if EditText is empty

Here's the code I'm using:

EditText ii1 = (EditText)findViewById(R.id.i1); String iii1= (String)ii1.getText().toString(); ind1=Double.parseDouble(iii1);

I need that ind1 acquire value 0 if the EditText is empty. I already tried using if (ii1 = "") and if (iii1= ""), but didn't work. How can I do that??

Thanks!

UPDATE:

Got it using this code: if (iii1.equals("")) {ind1=0;} else {ind1 = Double.parseDouble(iii1);}

Thank you all for the help!

Upvotes: 0

Views: 529

Answers (2)

mariusz-
mariusz-

Reputation: 209

You sould compare strings with

iii1.equals("")

instead of iii1 = ""

And please, names like "iii1" are suicide sooner or later...

Upvotes: 0

Rilcon42
Rilcon42

Reputation: 9765

This should work if(ind1 == null || ind1.isEmpty()){ind1=0;}

Upvotes: 1

Related Questions