-1

Here is my code:

if(editText.getText().toString() == ""){
   editTextBenefaction.setText("0");
}

Why he didn't work?

nikitko43
  • 79
  • 1
  • 2
  • 3

2 Answers2

10

Change it to

if(editText.getText().toString().equals("")){

In Java .equals() is used to compare if they have the same value and "==" is used to determine if they reference the same object.

An even better way is to use

if("".equals(editText.getText().toString())){

because this will protect against a NPE.

Java String Docs

codeMagic
  • 44,549
  • 13
  • 77
  • 93
2

Do not use == with Strings use equals()

if(editText.getText().toString().equals("")){
   editTextBenefaction.setText("0");
}
Manishika
  • 5,478
  • 2
  • 22
  • 28