0

Im new to programming. I have to make a palindrome checker program for my class. I'm having trouble iterating this program, it needs to keep asking the user to enter a phrase until they enter "done". Would a do loop be appropriate?

This is what my output should look like:

Enter phrase: A man, a plan, a canal, Panama! That is a Palindrome.

Enter phrase: Once upon a time That is NOT a Palindrome.

Enter phrase: No 'x' in Nixon. That is a Palindrome.

Enter phrase: done

and this is my work so far

      import java.util.Scanner;
      class Palindrome2 {
      public static void main(String args[]) {
      
       
       
       Scanner in = new Scanner(System.in);
    
      String reverseString="";
      String inputString;
      
      System.out.println("Palindrome Checker");
      
          
      do {
        System.out.println("Enter a phrase: ");
        inputString = in.nextLine();
        inputString = inputString.replaceAll("[^A-Za-z]+", "").toLowerCase(); 
        
        char[] characters = inputString.toCharArray();
       
    
   
      for ( int i = characters.length - 1 ; i >= 0 ; i-- )
         reverseString = reverseString + inputString.charAt(i);
     
      if (inputString.equals(reverseString))
          System.out.println("That is a palindrome.");
       else
          System.out.println("That is not a palindrome.");
       
      } while (inputString.toLowerCase().equals("done"));
    
      
 
    in.close();
      
   }
}

amsh
  • 3,097
  • 2
  • 12
  • 26
dan
  • 1
  • 1
    I think you also consider the case or the words. Could simply use `if (inputString.equalsIgnoreCase(reverseString))` – Scary Wombat Oct 27 '20 at 04:41
  • 1
    yes, a `do while` loop is ideal for this application, but you want the the program to continue execute while the inputString doesn't equals to "done" i.e. `while (!inputString.toLowerCase().equals("done"))`. You missed the `!` – chakwok Oct 27 '20 at 04:44
  • Does this help: [How to get out of while loop in java with Scanner method “hasNext” as condition?](https://stackoverflow.com/questions/10490344/how-to-get-out-of-while-loop-in-java-with-scanner-method-hasnext-as-condition) – Abra Oct 27 '20 at 07:41

1 Answers1

0

You don't need to check if the "done" string is a palindrome or not so, I would recommend using while loop instead of do while. Secondly, the condition should have a not i.e. ! and Enter a phase: should be a print statement and not println, as per your sample run. Your code would look like this:

import java.util.Scanner;
      class Palindrome2 {
      public static void main(String args[]) {
      
       
       
       Scanner in = new Scanner(System.in);
    
      String reverseString="";
      String inputString;
      
      System.out.println("Palindrome Checker");
      System.out.print("Enter a phrase: ");
      inputString = in.nextLine();
      inputString = inputString.replaceAll("[^A-Za-z]+", "").toLowerCase();
      
          
      while (!inputString.equals("done")){ 
        
        char[] characters = inputString.toCharArray(); 
    
   
      for ( int i = characters.length - 1 ; i >= 0 ; i-- )
         reverseString = reverseString + inputString.charAt(i);
     
      if (inputString.equals(reverseString))
          System.out.println("That is a palindrome.");
       else
          System.out.println("That is not a palindrome.");
      
      System.out.print("Enter a phrase: ");
      inputString = in.nextLine();
      inputString = inputString.replaceAll("[^A-Za-z]+", "").toLowerCase();
       
      }
    
      
 
    in.close();
      
   }
}
amsh
  • 3,097
  • 2
  • 12
  • 26