Answered step by step
Verified Expert Solution
Link Copied!

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.

image text in transcribed

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

image text in transcribedimage text in transcribed

palindrome? Yes : Process finished with exit code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions

Question

b. What groups were most represented? Why do you think this is so?

Answered: 1 week ago

Question

3. Describe phases of minority identity development.

Answered: 1 week ago

Question

5. Identify and describe nine social and cultural identities.

Answered: 1 week ago