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.

I am trying to write a program that checks a user input

--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,

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 =

palindrome? Yes : Process finished with exit code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!