Question: I am trying to write a program that checks a user input is a palindrome. --my code-- package ca.J105_4.terminal; import java.util.Scanner; public class J105_4 {
I am trying to write a program that checks a user input is a palindrome.

--my code--
package ca.J105_4.terminal; import java.util.Scanner; public class J105_4 { public static void main(String args[]) { String string1, string2, reverse = ""; Scanner in = new Scanner(System.in); System.out.println("palindrome? "); string1 = in.nextLine(); string2 = string1.replaceAll("\\s", ""); int length = string2.length(); String a = "0"; for ( int i= length - 1; i >= 0; i-- ) reverse = reverse + string2.charAt(i); if (string2.equals(a) && string2.equals(reverse)){ System.out.println("Yes "); System.out.println(string1+ " : " +string1); } else if(string2 !=(a) && string2 !=(reverse)) { System.out.println("No "); }else { System.out.println(" Thank you "); } } } when I run the program and input a palindrome phase like my gym, it says "No" when it is supposed to say "Yes" and prints out string 1 and string 2. However, when I input 0 says yes when it is supposed to say "Thank you".
Is there any way to fix this?
Thanks,

![{ public static void main(String args[]) { String string1, string2, reverse =](https://s3.amazonaws.com/si.experts.images/answers/2024/09/66d9c00ae8cf4_50666d9c00a9aab6.jpg)
palindrome? Yes : Process finished with exit code
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
