-1

I don't understand what's wrong with the IndexOf function ???

public String[] PseudoExisteTest() {
  // looking if an XML tag contain "OK"
String exampleText = "<result>OK</result>";

int ind1;
int ind2;

String returnTable[] = new String[4];
String tag="result";
String textresult;

ind1=exampleText.indexOf("<"+tag+">");   // 0
ind2=exampleText.indexOf("</"+tag+">");  // 10

textresult=exampleText.substring(ind1+tag.length()+2, ind2);

    if ((textresult=="OK")) {     // YES  => Normally we pass here (="OK") !  
        returnTable[0]="It'OK";
        }
    else {
        returnTable[0]="Not, value is : "+textresult+"!";  // Not, value is : OK !!! ????? 
    }
    returnTable[1]="blabla";

    return returnTable;
}       

The value is "OK" but on the condition, that's don't works well ???? Is anybody can help me ?

Thanks in advance.

user3546139
  • 1
  • 1
  • 3

1 Answers1

2

The problem is that you're using == to do a comparison of Java Strings. For objects in Java, which includes Strings, == tests whether the objects are the same. Instead, say textresult.equals("OK") or textresult.equalsIgnoreCase("OK").

As the comments say, see also How do I compare strings in Java?

Community
  • 1
  • 1
David Koelle
  • 20,726
  • 23
  • 93
  • 130