Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.equals() always return true

K want to compare two strings but the equals() method always return true , so the if statement always runs. why this happens?

resultString = "Test1";
String CompleteString = "Test";
if(CompleteString.equals(resultString));
{
   rowView.setBackgroundResource(R.color.listselect_red);
}
like image 512
Memento Avatar asked Dec 14 '13 10:12

Memento


1 Answers

if(CompleteString.equals(resultString)); <-- remove the ;

Your code is the equivalent to :

if(CompleteString.equals(resultString))
{
  //empty block
}
{
   rowView.setBackgroundResource(R.color.listselect_red);
}

So if equals returns true, the empty block will be executed, and after the second block will be always executed whatever the if was false or true.

like image 142
Alexis C. Avatar answered Sep 30 '22 05:09

Alexis C.