Reputation: 1574
I'm trying to do an operation when two strings are different. The strings are actually time and am checking to see if the new time i obtained from remote database is different.
I repeatedly get time from database and have a variable which contains the previous time to check if the new time is same as old time. If not I do an operation. I see that the time is exactly same but still the code is executed.
I even check the length of the two strings and they are exactly same. I Toast
the string1.equals(string2)
and get false though they are same.
if( (!lastUpdated.equals(prevTime))) {
swipe(Integer.parseInt(x1.toString()), Integer.parseInt(y1.toString()), Integer.parseInt(x2.toString()), Integer.parseInt(y2.toString()));
CodeType.setLength(0);
Toast.makeText(getApplication(), " lastUp " + lastUpdated + ", prevtime " + prevTime, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), ""+lastUpdated.equals(prevTime), Toast.LENGTH_SHORT).show();
updatePrevTime(lastUpdated);
}
Upvotes: 0
Views: 2108
Reputation: 187
Explanation
In case where strings are not strictly of String class, there could be problem in .equals() method. Recently I have encountered same problem, one was of String class and the other was from EditText.getText(). If you don't pay attention closely, it could be the problem because getText() method actually returns CharSequence which is not same as String, it would have worked elsewhere in the program by auto conversion but not in .equal() . But using getText().toString() gave correct result because now I'm comparing two objects of strictly String class.
(Hoping it would be helpful for someone.)
Same problem happens when it comes from Stream classes as internally it deals with characters and also could be due to encoding. So check them out.
Upvotes: 0
Reputation: 309
If you're using StringBuffer
, equals
will not compare the two String
s.
Do this:
yourStrBuffer.toString().equals(prevTime.toString())
Upvotes: 2
Reputation: 3083
Use TextUtils class:
You can try to use TextUtils.equals(CharSeq1, Charseq2) in case you might have missed some cases in your comparison.
The method from TextUtils takes care of the null cases.
If the return method it is still the same then you can try using by char(maybe that fits better than using the standard equals):
for (int i = 0; i < length; i++) {
if (a.charAt(i) != b.charAt(i)) {
return false;
}
}
You can find details about the class here:
https://android.googlesource.com/platform/frameworks/base/+/b2c4f0bf11f38fd31d80f1256c89b9db043a2929/core/java/android/text/TextUtils.java
Upvotes: 0
Reputation: 30
How are times being formatted to string? Do your values contain letters like timezone or period (am/AM)? Can you use the same formatter for both values or use equalsIgnoreCase()
?
Upvotes: 0
Reputation: 2136
There is trailing or leading whitespace in string.
Try this: lastUpdated.trim().equals(prevTime.trim())
This should be true if both are equal.
Upvotes: 0
Reputation: 4345
Check your lastUpdated and prevTime.
Here is simple example of equals() . Try this should work.
String Str1 = "Hello";
String Str2 = "Hello";
boolean retVal;
retVal = Str1.equals( Str2 );
System.out.println("Returned Value = " + retVal );
output
Returned Value = true
Upvotes: 0