-2

I have this code and to me it seems like the logic is correct. When the value.toString() is entered it prints out 0123456789 just like it should because that is the value I entered in the editText field. Is there something simple that I am missing here or is this fine and I will have to look further afield.

Editable value = input.getText();
Log.i("Password entered: ", value.toString());
if(value.toString() == "0123456789"){
    Log.i("Password entered: ", "yay it is working!");
} else {
    Log.i("Password entered: ", "it is incorrect");
}
Nick
  • 2,593
  • 3
  • 30
  • 59

4 Answers4

8

Use .equals to compare strings.

     if(value.toString().equals("0123456789"))
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

== tests for reference equality.

.equals() tests for value equality.

value.toString().equals("0123456789")

will work as intended.

Akinakes
  • 657
  • 4
  • 10
0

You can compare Your String with Following Code

value.toString().equalsIgnoreCase("0123456789");

It gives you best result.

Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
-1

Try this:

((value.toString()).compareTo("0123456789") == 0){
     //Do somthing here
}
Long Dao
  • 1,341
  • 4
  • 15
  • 33
  • This is more unreadable than .equals() and should only be used if one is really interested in the order of the strings, but not if just checking for equality. – Bananeweizen Aug 05 '13 at 04:39
  • In my opinion, it is much stronger to compare 2 strings than the equals() method. I can't see why you said it is unreadable??? – Long Dao Aug 05 '13 at 04:44
  • It is unreadable, because everyone maintaining that code will have to "decode" the part "compareTo(...) == 0" meaning "equals". While with the solution using "equals()" you can read the code like English literature. Also it is quite easy to write "compareTo(...) != 0" or "compareTo(...) < 0" or something else by mistake and not notice that. For the "equals(...)" call that is basically impossible. Therefore this will avoid more bugs in the long run. – Bananeweizen Aug 05 '13 at 04:54