Reputation: 11
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
Upvotes: 0
Views: 730
Reputation: 78
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...");
}
}
}
Upvotes: 1
Reputation: 1253
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.
Upvotes: 0
Reputation: 26
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
Upvotes: 0
Reputation: 521093
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.
Upvotes: 0