Android Java : what's wrong with indexOf? [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
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.

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?

Related

Comparing Strings not Working in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm importing a string from a file and the string is "Computer_Made". If I execute this code, though, it does not print "The computer is already made!" Any ideas?
if (data=="Computer_Made")
{
computer=true;
System.out.println("The computer is already made!");
}
You should use .equals for string comparison!!
if (data.equals("Computer_Made"))
{
computer=true;
System.out.println("The computer is already made!");
}
Refer here for more info
In Java, String are compared using equals or equalsIgnoreCase method.
Using == is reference equality and will rarely be the same.
Try instead:
if (data.equals("Computer_Made"))
== will only work in example like this:
String a = "Ha";
String b = a;
System.out.println("a==b :" + a==b); //prints a==b : true

If() inside WHILE-BufferedReader.readLine() does not work [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
UPDATE:
Thank you all for your answers, especially pertaining to .equals().
The only detail is that the "msgCode = ..." and "msgValue = ..." statements are enough to return an empty stirngBuilder. I.e., I don't even have to declare the IF statement to make it stop working.
Any clues?
ORIGINAL:
Please let me know why StringBuilder returns nothing (perhaps doesn't even work) when I include the rest of the code (besides stringBuilder.append(...)) inside the while().
When I include just stringBuilder.append(...), then there is a return value.
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString + "\n");
// analyze the first 3 characters in the message
String msgCode = receiveString.substring(0, 3);
Number msgValue = Integer.parseInt(receiveString.substring(4, receiveString.length()-4));
// use IF-ELSE since SWITCH doesn't work with String
if (msgCode=="ATT") {
dataATT[2*dataATTcount+1] = msgValue;
dataATTcount++;
} else {
dataMED[2*dataMEDcount+1] = msgValue;
dataMEDcount++;
}
}
Thanks
use
msgCode.equals("ATT")
instead of
msgCode=="ATT"
with '==' you compare the references of the string and not the string itself.
Never-ever use == for Java string comparison. Use equals().
P.S. Okay, there might be some cases where == would be suitable. But for the vast majority of situations, equals() is the way to go.

Java Check If Two JTextField Has The Same Content [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have two JTextFields txf1 and txf2.
In both of them I input the same content (for example: "test").
I made and If statement:
if (txf1.getText() == txf2.getText()) {
System.out.println("Equal");
} else {
System.out.println("Error");
}
Why it prints out the error message? I even made a System.out.println(txf1.getText()) and System.out.println(txf2.getText()) and the same looks equal, but prints out the error message?
String comparison in Java is done using String#equals, using == means you are comparing the memory reference of the objects, which won't always return true when you think it should.
Try something more like....
if (txf1.getText().equals(txf2.getText())) {
...instead
Also you can use this good practice which makes your text box entries efficient.
if (txf1.getText().trim().equals(txf2.getText().trim())) {
Use the equals method to compare Strings. == only compares the object reference. equals compares the actual content of the Strings.
Your code should be something like this:
if (txf1.getText().equals(txf2.getText())) {
System.out.println("Equal");
} else {
System.out.println("Error");
}

Jsoup: String element.attr() doesn't work in if() [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm not experienced in Java, and I have a problem.
Using Jsoup, I have an element called td. If I do:
String attr = td.attr("class");
System.out.println(attr);
The output is "free", which is perfectly alright. If I do:
String attr = td.attr("class");
if (attr == "free") {
System.out.println("freedom!");
}
There is no output!
Does anyone know how to solve this problem?
Thanks in advance.
You have to compare the string using the equals method as == compare references, not strings contents.
String attr = td.attr("class");
if (attr.equals("free")) {
System.out.println("freedom!");
}

If comparison in Java miss [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have a portion of code that should do that:
1) Take the name of a color from keyboard
2) Give that color to an object in wich i have another object declaration
3) Decrement from 2 to 1 the correspondant line of the array of 2.
In the main class i want to memorize the color in a variable TemporaryColor.
Scanner input = new Scanner(System.in);
String TemporaryColor = input.nextLine();
playerOrd[1].DecrementoSegnalino(TemporaryColor);
playerOrd is a class that contains this method:
public void DecrementoSegnalino(String color) {
SegnalinoScommessaGiocatore.decrementaSegnaliniScommessa(color);
}
SegnalinoScommessaGiocatore have this array:
private int[] numeroSegnaliniScommessa = {2,2,2,2,2,2};
and this method:
public void decrementaSegnaliniScommessa(String color) {
if (color.equalsIgnoreCase("Black") numeroSegnaliniScommessa[0]--;
if (color.equalsIgnoreCase("Blue") numeroSegnaliniScommessa[1]--;
if (color.equalsIgnoreCase("Green") numeroSegnaliniScommessa[2]--;
if (color.qualsIgnoreCase("Red") numeroSegnaliniScommessa[3]--;
if (color.equalsIgnoreCase("Yellow") numeroSegnaliniScommessa[4]--;
if (color.equalsIgnoreCase("White") ) numeroSegnaliniScommessa[5]--;
}
There is a problem passing the string that i write with Keyboard...
If i use this in the beginning:
playerOrd[1].DecrementoSegnalino("Black");
It works!
What's the problem?
Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of
if (fu == "bar") {
// do something
}
do,
if ("bar".equals(fu)) {
// do something
}
or,
if ("bar".equalsIgnoreCase(fu)) {
// do something
}

Categories

Resources