I get a single String value using the Scanner class in Java. The value is stored in the "name" field. I use the StringBuilder class for my boolean operation and finally save one string value. The name of the field is "sb". Finally, both fields have the same meaning. I tried comparing the values ββwith .equals (), and that didn't help me. Please pass the code below and please help me overcome this problem.
public static void main(String[] args) { System.out.println("Enter String: "); String name = new Scanner(System.in).nextLine(); StringBuilder sb = new StringBuilder(); for (int i = name.length() - 1; i >= 0; i--) { sb.append(name.charAt(i)); } System.out.println(name); System.out.println(sb); if (name.equals(sb)) { System.out.println(name + " --> Palindrome"); } else { System.out.println(name + " --> Not a Palindrome"); } }
Output:
Enter the string:
madam
madam
madam
madam β Not a palindrome
source share