public class testing {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
sb.append(str);
sb1.append(str);
sb1.reverse();
if(sb == sb1) {
System.out.println("yes");
}else if(sb != sb1) {
System.out.println("no");
}
System.out.println(sb.length());
System.out.println(sb1.length());
sc.close();
}
}
Everything working fine including string reverse, but always getting no as result.
You will always get no
because StringBuilder
creates new object & as you are using ==
for comparison, it compares reference only not content of StringBuilder object, so if you want to compare content you have to use .equals()
method after applying .toString()
on StringBuilder object.