-4

Possible Duplicate:
How do I compare strings in Java?
spinner If Strings** == not working

public void btnRecordTrip_Clicked(View v)
{
    Button b = (Button)v;
    String currentText = b.getText().toString();
    if (currentText == "Record Trip")

The value of currentText is "Record Trip" yet the it compares unequal. I tried .trim() with the same result. The two strings are equal but they compare unequal. What's going on?

Community
  • 1
  • 1
Dean Blakely
  • 3,535
  • 11
  • 51
  • 83

2 Answers2

3

Use .equals() to compare strings in Java

if (currentText.equals("Record Trip"))
Musa
  • 96,336
  • 17
  • 118
  • 137
0

In Java, when you are comparing objects, == operator is used for identity check. For equality check you should use .equals() method.

In your case, even though the two string objects are equal value wise, they are two different objects.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • Does this also go for checking equality on Integers, Booleans, etc? In other words should "==" never be used for checking for equal value? – Dean Blakely Oct 28 '12 at 17:40
  • @user1058647: " In other words should "==" never be used for checking for equal value?", Yes, in case of objects. – Bhesh Gurung Oct 28 '12 at 18:11