+ 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 Answers
+ 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 đ