Search code examples
javapalindrome

Palindrome program is not giving the correct output in Java


  • List item

I created a Palindrome program in Java that takes a word or words entered by the user and puts them into an array. It then takes the reversed input and puts it into another array. I then check to see if the two words are the same.

My problem is that the program is changing one of the letters so that it is always true and it's iterating too many times. Any ideas as to what in my code is causing this?

public static boolean isPalindrome(char[] a, int used){
char[] original = a;
int newNumber = used - 1;
for(int i = 0; i <= newNumber; i++){
  a[i] = a[newNumber - i ];
  System.out.println("Your original word was: " + String.valueOf(original));
  System.out.println("Backwards, your word is: " + String.valueOf(a));

  }
if(String.valueOf(original).equalsIgnoreCase(String.valueOf(a))){
  System.out.println("Your word or words are palindromes.");

}else{
  System.out.println("Your word or words are not palindromes");
}
return(String.valueOf(original).equalsIgnoreCase(String.valueOf(a)));
}

public void userInput(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a word or words that you believe are palindromes: ");
word = keyboard.next();
word = word.replaceAll("\\W","");
int length = word.length();
char[] p = new char[length];
for(int i = 0; i < length; i++){
  p[i] = word.charAt(i);
}
Palindrome.isPalindrome(p, length);

}

This is my output when I type in "Candy."

Your original word was: yandy
Backwards, your word is: yandy
Your original word was: ydndy
Backwards, your word is: ydndy
Your original word was: ydndy
Backwards, your word is: ydndy
Your original word was: ydndy
Backwards, your word is: ydndy
Your original word was: ydndy
Backwards, your word is: ydndy
Your word or words are palindromes

Thanks to everyone that answered! Here is the correct code that works, although not as efficient as it could be:

  public static boolean isPalindrome(char[] a, int used){
    char[] original = a;
    char[] newArray = new char[used];
    int newNumber = used - 1;
    for(int i = 0; i <= newNumber; i++){
      newArray[i] = a[newNumber - i ];
      }
     System.out.println("Your original word was: " + String.valueOf(original));
     System.out.println("Backwards, your word is: " + String.valueOf(newArray));

    if(String.valueOf(original).equalsIgnoreCase(String.valueOf(newArray))){
      System.out.println("Your word or words are palindromes.");

    }else{
      System.out.println("Your word or words are not palindromes");
    }
    return(String.valueOf(original).equalsIgnoreCase(String.valueOf(newArray)));
    }

Solution

  • Problem is with your code a[i] = a[newNumber - i ]; you are changing the existing array.

    First create new array and then put your reverse string in that.

        public static boolean isPalindrome(char[] a, int used) {
        char[] newA = new char[a.length];
        int newNumber = used - 1;
        for (int i = 0; i <= newNumber; i++) {
            newA[i] = a[newNumber - i];
            System.out.println("Your original word was: " + String.valueOf(a));
            System.out.println("Backwards, your word is: " + String.valueOf(newA));
        }
        if (String.valueOf(String.valueOf(newA)).equalsIgnoreCase(String.valueOf(a))) {
            System.out.println("Your word or words are palindromes.");
    
        } else {
            System.out.println("Your word or words are not palindromes");
        }
        return (String.valueOf(String.valueOf(newA)).equalsIgnoreCase(String.valueOf(a)));
    }
    

    There are many other efficient ways to write this code.