Frank Longo Frank Longo -4 years ago 195
Java Question

Palindrome iterative checker

package Checkers;
import java.util.Scanner;
public class iterative
{
public static void main(String args[])
{
String reverseString="";
Scanner scanner = new Scanner(System.in);
String more = null;
do {
System.out.println("Enter a string to check if it is a palindrome:");
String inputString = scanner.nextLine();
String combined = inputString.replaceAll("[\\W]", "");
combined = combined.toLowerCase();

int length = combined.length();

for ( int i = length - 1 ; i >= 0 ; i-- )
reverseString = reverseString + combined.charAt(i);

if (combined.equals(reverseString))
System.out.println("Input string is a palindrome.");
else
System.out.println("Input string is not a palindrome.");
// Ask user to Evaluate another string
System.out.println();
System.out.print("Evaluate another string? (Y=Yes): ");
more = scanner.nextLine();
System.out.println();
}
while (more.equalsIgnoreCase("y"));
}

}


Output: Enter a string to check if it is a palindrome:
radar
Input string is a palindrome.

Evaluate another string? (Y=Yes): y

Enter a string to check if it is a palindrome:
radar
Input string is not a palindrome.

Evaluate another string? (Y=Yes):

I have this problem when I put a palindrome in like for example radar. Its a palindrome. put it in again and i get its not a palindrome. I don't whats wrong please help

Answer Source

Put the line

String reverseString="";

into

do { ...}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download