Search code examples
javapalindrome

How to split user input into char array or string array and then check if palindrome or not in java


So I am writing a program from my Java programming class that is asking to make a palindrome program. I have successfully made the program that runs clean with one word user input but I am stuck on how to check the three words that a user inputs individually. Here is my code so far. The lab objective is: check each word individually to see if it is a palindrome – if you find one, print it to the screen. I must follow these instructions: • Ask the user to supply three palindromes in one line

• Check each word individually to see if it is a palindrome – if you find one, print it to the screen – Hint: look at String’s toCharArray() method • Keep asking the user until they have supplied a set of words containing at least one palindrome.

import java.util.Scanner;
public class Palindrome {

private static Scanner scanUserInput;
private static String wordGuess, reverseWord ;
public static void main(String[] args) {
scanUserInput = new Scanner(System.in);
System.out.println("WELCOME TO THE PALINDROME CHECKER!!!");
while(true){
System.out.print("Please enter at least three words to check: ");
wordGuess = scanUserInput.nextLine();
reverseWord = "";

//String[] wordArray = wordGuess.split(","); I tried to use this way to split the inputs but no luck
char[] wordArray = wordGuess.toCharArray();
for(int x=wordArray.length-1;x>=0;x--){
    reverseWord = reverseWord+wordArray[x];

}
System.out.println(reverseWord);
if(wordGuess.equalsIgnoreCase(reverseWord))
{
    System.out.println("");
    System.out.println("You have found a Palindrome!!!");
    System.out.println("The Palindrome we found was "+reverseWord);
    break;
}
else{
    System.out.println("");
    System.out.println("You have not entered a Palindrome...");
    System.out.println("Please Try again...");

}

}//end of main
}
}

Thanks ahead for your time. Please get back to me with any documentation that would be of use for me to complete this lab or any other ideas. enter image description here


Solution

  • Your code will run fine if user inputs only a single word. But when you enter more than one word then till will give error. Example : input: racecar hello test your code will reverse this as : tset olleh racecar

    racecar hello test!= tset olleh racecar

    If the user input has delimiter as , then use

    String[] wordArray = wordGuess.split(",");
    

    Put your logic in a separate function:

    public checkPalindrome(String word){
        reverseWord="";
        for(int x=word.length-1;x>=0;x--){
            reverseWord = reverseWord+wordArray[x];
    
        }
        System.out.println(reverseWord);
        if(wordGuess.equalsIgnoreCase(reverseWord))
        {
            System.out.println("");
            System.out.println("You have found a Palindrome!!!");
            System.out.println("The Palindrome we found was "+reverseWord);
            break;
       }
       else{
            System.out.println("");
            System.out.println("You have not entered a Palindrome...");
            System.out.println("Please Try again...");
    
      }
    
    } 
    

    Call this function from your main method 3 times and pass all the "," separated string one by one.

    checkPalindrome(wordArray[0]);
    checkPalindrome(wordArray[1]);
    checkPalindrome(wordArray[2]);
    

    Output :

    WELCOME TO THE PALINDROME CHECKER!!!
    Enter number of Strings with spaces to check palindrome
    abba acac adda abaa
    Pallindrome Strings
    abba
    adda