+ 2
Java
What am I doing wrong here? String title1 = new String(“cse220”); String title2 = “cse220”; if (title1 == title2) { System.out.print(“String using comparison “==“ sign works”); } else { System.out.print(“String comparison using “==“ does NOT work”); } if (title1.equals(title2)) { System.out.print(“String comparison using “equals” method works”); } else { System.out.print(“String comparison using “equals” method does NOT work”); }
3 odpowiedzi
+ 4
Yoseph
To add double quotes to your string, you can use escape sequence (\").
Otherwise it will cause error.Like you have given ("String comparison using "==" does NOT work");
Here the text inside double quotes are treated as string.The two strings will be,
1."String comparison using"
2."does NOT work"
But == is not in any string it will cause error.
Here is the modified code
String title1 = new String("cse220");
String title2 = "cse220";
if (title1 == title2)
{
System.out.print("String using comparison \"==\" sign works");
}
else
{
System.out.print("String comparison using \"==\" does NOT work");
}
if (title1.equals(title2))
{
System.out.print("String comparison using equals method works");
}
else
{
System.out.print("String comparison using equals method does NOT work");
}
Thnx
+ 1
Awesome thank you so much !! Jewel
0
You're welcome Yoseph 👍