Question

  • 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)));
    }
Était-ce utile?

La solution 2

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.

Autres conseils

In your Palindrome function:-

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));

          }

here you are replacing the existing array.

You can even try the following code:-

String original, reverse="";
              Scanner in = new Scanner(System.in);

              System.out.println("Enter a string to check if it is a palindrome");
              original = in.nextLine();

              int length = original.length();

              for ( int i = length - 1 ; i >= 0 ; i-- )
                 reverse = reverse + original.charAt(i);

              if (original.equals(reverse))
                 System.out.println("Entered string is a palindrome.");
              else
                 System.out.println("Entered string is not a palindrome.");

If you want to check palindrome in this easy way, you can try with StringBuilder.

public static boolean isPalindrome(String input) {
    StringBuilder sb=new StringBuilder(input);
    return input.equals(sb.reverse().toString());
}

In your case change your isPalindrome method as follows

 public static void isPalindrome(char[] a, int used){
    char[] newArr = new char[a.length]; // create a new array in same length as a
    int newNumber = used - 1;
    System.out.println("Your original word was: " + String.valueOf(a));
    for(int i = 0; i <= newNumber; i++){
        newArr[i] = a[newNumber - i ];
    }
    System.out.println("Backwards, your word is: " + String.valueOf(newArr));
    if(String.valueOf(newArr).equalsIgnoreCase(String.valueOf(a))){
        System.out.println("Your word or words are palindromes.");
    }else{
        System.out.println("Your word or words are not palindromes");
    }
}
  1. there is no need to copy the array (you only copied the referenz), only creat a new array with the same size and use a as the original array.

    char[] reverse = new char[used];
    
  2. put the system.out, out of the for loop:

    for(int i = 0; i <= newNumber; i++){
        reverse[i] = a[newNumber - i ];
    }
    System.out.println("Your original word was: " + String.valueOf(a));
    System.out.println("Backwards, your word is: " + String.valueOf(reverse));
    

the whole isPalindrom function:

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

    if(String.valueOf(reverse).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(reverse).equalsIgnoreCase(String.valueOf(a)));
}

The problem is in isPalindrome method where you assigned

char[] original = a;

The char[] variables are references. By setting original = a, you're copying the reference, so now both the arrays are pointing to the same chunk of memory.

Now, when you modify a, original also gets modified because both are pointing to same memory.

You can fix this by creating a new instance as shown below:

    char[] original = new char [a.length];
    System.arraycopy( a, 0, original, 0, a.length );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top