Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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,
palindrome? Yes : Process finished with exit code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started