StringBuilder reverse does not produce a new StringBuilder instance. It causes the underlying characters of the current StringBuilder to be reversed. So,
String a = s.reverse().toString();
String b = s.toString();
The second s.toString() is operating on the reversed StringBuilder.
you have to do
String original = s.toString();
String reversed = s.reverse().toString();
return original.equals(reversed);
Answer from Thiyagu on Stack OverflowVideos
StringBuilder reverse does not produce a new StringBuilder instance. It causes the underlying characters of the current StringBuilder to be reversed. So,
String a = s.reverse().toString();
String b = s.toString();
The second s.toString() is operating on the reversed StringBuilder.
you have to do
String original = s.toString();
String reversed = s.reverse().toString();
return original.equals(reversed);
Here's one using only 2 StringBuilders instead of 3 and doesn't use the built-in reversed method. Instead, the while loop traverses through the linked list, appends the current node to the original StringBuilder, and inserts the current node at the front of the reversed StringBuilder. So the built-in insert method helps with reversing the order of the nodes values :
public boolean isPalindrome(ListNode head) {
StringBuilder original = new StringBuilder(), reversed = new StringBuilder();
ListNode curr = head;
while (curr != null) {
original.append(curr.val);
reversed.insert(0, curr.val);
curr = curr.next;
}
return original.toString().equals(reversed.toString()) ? true : false;
}
I think you are on the right lines, just a few things I'd change: Firstly we have the ability to specify the length of the StringBuilder because we know it is going to be the same length as the String we are reversing.
It is not necessary to loop through the entire array, just half the array will suffice performing operations which are essentially just switching the characters. So the right-most character is now the left-most character, this saves a little computation.
public static void main(String[] args) throws IOException {
String str = "Reverse me";
StringBuilder printStr = new StringBuilder();
printStr.setLength(str.length());
for(int i =0; i<str.length()/2;i++){
char left = str.charAt(i);
final int fromRight = str.length() - i - 1;
printStr.insert(i, str.charAt(fromRight));
printStr.insert(fromRight, left);
}
}
Here is a visual representation of how to character switching essentially works: More information can be found here

OK, so you made multiple mistakes, you are trying to access indexes by using parenthesis, you are not iterating over whole string as your loop condition is i > 0 and it should be i >= 0, and your loop is just wrong (disregard parenthesis), you can combine append() and charAt() methods to do what you need:
for (int i = str.length()-1; i >= 0; i--) {
printStr.append(str.charAt(i));
}
We go from last index of your string up to index 0, so we start from the end of the string, and we append each character to our StringBuilder. In the end you have your reversed string.
Your first problem might be that straight uses index j, while the rest of the code uses index i.
Here is your code as an MCVE (Minimal, Complete, and Verifiable example):
StringBuilder stringBuilder = new StringBuilder("tor");
String straight = stringBuilder.toString();
String reverse = stringBuilder.reverse().toString();
System.out.println("stringBuilders[i].reverse().toString() = " + stringBuilder.reverse().toString() +
" reverse = " + reverse +
" straight = " + straight +
" rev == str? " + reverse.equals(straight));
Output is:
stringBuilders[i].reverse().toString() = tor reverse = rot straight = tor rev == str? false
That is not the output you showed.
Anyway, your main problem is that reverse():
Causes this character sequence to be replaced by the reverse of the sequence.
So, your first call to reverse() makes the stringBuilder have the value rot, which is also the value assigned to the reverse variable.
Your second call to reverse() in the print statement reverse it back to the value tor, which is then the value printed.
stringBuilders[i].reverse().toString() = rot, reverse = tor,
StringBuilder is mutable so each time you call reverse() it flips the characters end to end.