-1

This always returns the value of "else"...

   public void onClick(View view) {

            System.out.println("Palabra1= "+palabraDes.getText());
            System.out.println("Palabra2= "+palabraOrd.getText());

            String pla1 = palabraDes.getText().toString();
            String pla2 = palabraOrd.getText().toString();



            if (pla1 == pla2) {

                ttsManager.initQueue("Resultado correcto, buen trabajo.");

            } else

                ttsManager.initQueue("Incorrecto, intente de nuevo.");

        }
    });

Launching app: I/System.out: Palabra1= perro Palabra2= perro

Cory Roy
  • 5,379
  • 2
  • 28
  • 47
  • Instead of using `==` operator, you should use the `equals()` method when comparing objects e.g. `pla1.equals(pla2)` – Igor Aug 01 '19 at 00:08

6 Answers6

1

You can't use == to compare strings. You will have to do if (string1.equals(string2))

   public void onClick(View view) {

        System.out.println("Palabra1= "+palabraDes.getText());
        System.out.println("Palabra2= "+palabraOrd.getText());

        String pla1 = palabraDes.getText().toString();
        String pla2 = palabraOrd.getText().toString();



        if (pla1.equals(pla2)) {

            ttsManager.initQueue("Resultado correcto, buen trabajo.");

        } else

            ttsManager.initQueue("Incorrecto, intente de nuevo.");

    }
});
Cory Roy
  • 5,379
  • 2
  • 28
  • 47
alex87
  • 419
  • 3
  • 11
0

You need to use Equals() instead of ==

0

Both equals() and == are used to compare objects to check for equality however the == checks if both objects point to the same memory location whereas .equals() evaluates values in the objects.

In your case you have to use String.equals

Replace

if (pla1 == pla2)

with

if (pla1.equals(pla2))
Hayssam Soussi
  • 903
  • 7
  • 16
0

My problem was that when the text string passed, it came with a space (\ n), then all the forms worked.

0

Cannot use == operator for strings

 Use if(pla1.equals(pla2))

    or

 Use if(pla1.equalsIgnoreCase(pla2))

 instead of if(pla1 == pla2) {
Pradeepvina
  • 90
  • 1
  • 7
0

Solution:

if (pla1.equals(pla2)) {

   ttsManager.initQueue("Resultado correcto, buen trabajo.");

} else

   ttsManager.initQueue("Incorrecto, intente de nuevo.");
}

In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:

Main difference between .equals() method and == operator is that one is method and other is operator.

We can use == operators for reference comparison (address comparison) and .equals() method for content comparison.

In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.

If a class does not override the equals method, then by default it uses equals(Object o) method of the closest parent class that has overridden this method.

I hope it will help you!

Xavi Jimenez
  • 314
  • 1
  • 3
  • 16