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

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!");
}

Related

String concatenation showing unexpected behavior [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
Bellow is snippet of my code. I just want to know why if condition is not being executed even if variable "ans" has value 2020.
private static void solve() throws Exception {
String str="20";
String str1="20";
String ans=str+str1;
if(ans=="2020")
System.out.println("matched");
else System.out.println("Not matched");
System.out.println(ans);
}
ans.equals("2020")
this should work.

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

Java - String Manipulation [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want to ask a question.
I feel so confused about my own coding because i think it is correct.
This is the issue.
public static void main(String[] args) {
String x = "Robert : Hi There";
String y = "Robert";
System.out.println(x.substring(0, x.indexOf(":")).trim());
if(x.substring(0, x.indexOf(":")).trim() != y){
System.out.println("Pass");
}
else
{
System.out.println("Not Pass");
}
}
This gave me output:
Robert
Pass
I want the output is "Not Pass" but why did my coding gave another result.
I hope you can tell what is wrong.
Thank You.
You compare string objects. So you have to ue the equals method:
if(x.substring(0, x.indexOf(":")).trim().equls(y)){

Why does == return true for a String comparison? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have previously asked a question about comparing 2 strings and was told that I should always use .equals.
However, I do not understand why this then works:
String y= "Mary";
String x= "Mary";
System.out.print(x==y);
This will print true, and I do not understand why.
Because those two String(s) have the same reference identity, and that is because they came from the String intern pool. If you were to add a new String() to one of them, like so -
String y= "Mary";
String x= new String("Mary");
System.out.print(x==y);
You would get false.

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

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?

Categories

Resources