0

I am trying to compare two string in my application but it fails to compare. I don't know why it fails.

    public void processFinish(String output) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), output,Toast.LENGTH_LONG).show();

            String check="false";
            if(output==check){
                     //doing something here
                }else{
                    //something here
                }
   }

the string object output has the value "false" but always the else block is executed why? I Tried by changing the output=="false" to

output.equals(check)
output.equalsIgnoreCase(check)
output.contentEquals(check)

nothings works...

dharanbro
  • 1,327
  • 4
  • 17
  • 40

4 Answers4

12
public void processFinish(String output) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), output,Toast.LENGTH_LONG).show();

            String check="false";
            if(output.trim().equals(check.trim())){
                     //doing something here
                }else{
                    //something here
                }
}

your string getting some unwanted blank space from method trim() remove starting and end blank space :) and .equals is a object class method which compare two string :)

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
1

Since instances of String are object, so you can't comparison two objects with ==. To comparison two String, you have to use equals() or equalIgnoreCase() method.

Replace this

if(output==check){
    //doing something here
}else{
    //something here
}

with...

if(output.equals(check)){
    //doing something here
}else{
    //something here
}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
1

try the following

         if(output.contains(check)== true{
                 //doing something here
            }else{
                //something here
            }
WISHY
  • 11,067
  • 25
  • 105
  • 197
0

I think output.equals(check) must works

Or your description maybe not correct.May it is not a string of "false" but "false "?

First ,check their length,then use output.trim().equals(check).

tianwei
  • 1,859
  • 1
  • 15
  • 24