0

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");
}

    }
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
jocham
  • 33
  • 4
  • The problem is that you never alter the value of `stringIn`. In your loop, you'd need to do `stringIn = in.next();` – Matt Sep 27 '18 at 01:24

2 Answers2

0

A very good use-case to use do-while loop. You use this loop when you have to make sure that your statements are executed at least once. And the subsequent execution is executed only if it matches a condition. In this case, that condition would be validating your input.

    Scanner in = new Scanner(System.in);

    String prompt = "Input word to test: ";

    String stringIn;

    do {
        System.out.println(prompt);
        stringIn = in.nextLine();
        prompt = "Invalid input, try again.";
    }
    while (stringIn.matches("[a-zA-Z]+"));

If the input is non-numeric the while condition would be true and will make this loop run again, hence asking for new input. if the input is numeric the while condition will be false hence exit the while loop and will give you user input in stringIn variable.

Em Ae
  • 8,167
  • 27
  • 95
  • 162
0

change in.next(); in while loop to stringIn= in.next(); and after while loop add stringOut = new StringBuilder(stringIn).reverse().toString(); .

import java.util.Scanner;

    public class Test {

        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.");
                stringIn= in.next(); //stops infinite error loop
            }
            stringOut = new StringBuilder(stringIn).reverse().toString();
            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");
    }

        }
    }
Nawnit Sen
  • 973
  • 2
  • 7
  • 14