0

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

5
  • 1
    What's the problem with your code? Please be more specific. Commented Oct 5, 2017 at 6:49
  • Please include three word inputs for which your code is failing. Your code looks OK to me. Commented Oct 5, 2017 at 6:50
  • Pretend i am the user inputing the words and I input racecar, hello, test .... i need to check individually to check if it is palindrome Commented Oct 5, 2017 at 6:51
  • Maybe you just had to trim the whitespace at the end then? Commented Oct 5, 2017 at 6:52
  • So if i enter three words and one is a palindrome and the others are not how can i only display the palindrome ? I can split the inputs into arrays and compare then? or? Commented Oct 5, 2017 at 6:53

4 Answers 4

1

You can use split for this purpose and it will work.

String[] wordArray = wordGuess.split(" "); //or .split("<seperator used b/w words>") 

Now iterate the wordArray and check for palindrome individually for each word. In your code you are not checking each word individually

//wrong

for(int x=wordArray.length-1;x>=0;x--){ reverseWord = reverseWord+wordArray[x]; } 

// modified code

 while(true){ System.out.print("Please enter at least three words to check: "); wordGuess = scanUserInput.nextLine(); String[] wordArray = wordGuess.split(","); // char[] wordArray = wordGuess.toCharArray(); for (String word : wordArray) { reverseWord = ""; for(int x=word.length()-1;x>=0;x--){ reverseWord = reverseWord+word.charAt(x); } System.out.println(reverseWord); if(word.equalsIgnoreCase(reverseWord)) { System.out.println(""); System.out.println("You have found a Palindrome!!!"); System.out.println("The Palindrome we found was "+reverseWord); } else{ System.out.println(""); System.out.println("You have not entered a Palindrome..."); System.out.println("Please Try again..."); } } } 
Sign up to request clarification or add additional context in comments.

5 Comments

Now how am i supposed to do that I have researched all over the web on the .split() I wrote that in my code first I commented it out. I am just not sure how to check individually how am I doing the for loop wrong?
I have added the code here. It is working for single or multiple words separated by comma. It will check for each word and print if it is palindrome or not.
It almost worked however when user input is "hello, racecar, level" it does not pick up the palindrome @alok456
I changed the splits too .split(" ") and its working fine however it now doesn't break; the loop after it being true in the if statement once a palindrome is found @alok456
It depends on your use case, if you want to break the loop once you find the first palindrome word then put break , otherwise it will print all the palindromes in the given words. use while loop and its breaking condition properly as per your requirement.
0

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 

3 Comments

yes sir that is why i am asking how to fix it so i can compare individually if it is a palindrome
where do i call this in the main function ? in my while loop?
This worked @arunabhgosal however I can not break the loop when I get atleast one palindrome
0

Your logic looks completely fine to me. If you have the additional requirement of being able to accept a CSV list of words to check, then you can slightly modify your code as follows:

System.out.print("Please enter at least three words to check: "); String wordGuess = scanUserInput.nextLine(); String[] words = wordGuess.split(",\\s*"); for (String word : words) { String reverseWord = ""; char[] wordArray = word.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); } else { System.out.println(""); System.out.println("You have not entered a Palindrome..."); System.out.println("Please Try again..."); } } 

I think there was a problem with the pattern you were using to split. You were doing this:

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

But if you input a CSV list with spaces, then it would capture that whitespace as being part of the word and then probably would not have a palindrome. Also, you needed to loop over the input words, which you also were not doing.

Comments

0

Your question is:- The lab objective is Check each word individually to see if it is a palindrome – if you find one, print it to the screen.

you have to check every string that is palindrome or not, this is what I get from your question :-

here is the code for that solution:

import java.util.Scanner; import java.util.StringTokenizer; /** * Created by itt on 10/4/17. */ 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!!!"); System.out.println("Enter Strings with spaces to check palindrome"); wordGuess = scanUserInput.nextLine(); StringTokenizer tokens = new StringTokenizer(wordGuess); System.out.println("Pallindrome Strings"); while (tokens.hasMoreTokens()){ StringBuilder builder = new StringBuilder(); String token = tokens.nextToken(); builder.append(token); if(builder.reverse().toString().equals(token)) { System.out.println(token); } } } } 

Output:

WELCOME TO THE PALINDROME CHECKER!!! Enter Strings with spaces to check palindrome abba acac daad jaaj Pallindrome Strings abba daad jaaj 

if I am wrong, please correct me So I can improve on that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.