String.equals () always returns true

K I want to compare two lines, but the method equals()always returns true, so the if statement always works. Why is this happening?

resultString = "Test1";
String CompleteString = "Test";
if(CompleteString.equals(resultString));
{
   rowView.setBackgroundResource(R.color.listselect_red);
}
+4
source share
6 answers

if(CompleteString.equals(resultString)); <- delete ;

Your code is equivalent to:

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

So, if equals returns true, an empty block will be executed, and after the second block it will always execute regardless of whether falseor true.

+16
source

Remove ;after the if statement.

if(CompleteString.equals(resultString))
{
  rowView.setBackgroundResource(R.color.listselect_red);
}
+4
source

; if(CompleteString.equals(resultString));

; , if() returns false true , if(). , rowView.setBackgroundResource(R.color.listselect_red);.

resultString = "Test1";
String CompleteString = "Test";
if(CompleteString.equals(resultString))// removed `;`
{
   rowView.setBackgroundResource(R.color.listselect_red);
}
+2
source

it's because of ';' at the end of if. Just uninstall and it will work!

+2
source
if(CompleteString.equals(resultString));

It will not enter if the block looks like an empty condition :)

+2
source

This is because of the semicolon in the if condition ..

Correct code

resultString = "Test1";
String CompleteString = "Test";
if(CompleteString.equals(resultString))
{
   rowView.setBackgroundResource(R.color.listselect_red);
}
+2
source

All Articles