I am making one of my first java projects checking if a user input word (no numbers or symbols) is a palindrome. I used the method outlined here: https://stackoverflow.com/a/4139065/10421526 for the logic. The code works per-request but I am having trouble re-prompting the user after invalid input as it seems to "block" the ability of my code to accept valid input, printing my "retry" message. I have also tried the do while version but I ended up with formatting errors. I think the way I define my stringOut variable is giving me trouble but Im not sure how to change that without making a duplicate as eclipse says. Here is the closest I could get after dozens of tries going in circles:
import java.util.Scanner;
public class PalindromTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Input word to test: ");
String stringIn = in.nextLine();
String stringOut = new StringBuilder(stringIn).reverse().toString();
while (!stringIn.matches("[a-zA-Z]+")) {
System.out.println("Invalid input, try again.");
in.next(); //stops infinite error loop
}
if ((stringIn.equalsIgnoreCase(stringOut))) {
System.out.println("Palindrome detected");
System.out.println("You entered: " + stringIn);
System.out.println("Your string reversed is: " + stringOut);
} else {
System.out.println("Not a palindrome");
}
}
}