11 Replies Latest reply: Dec 14, 2008 7:19 PM by 843785 RSS

    palindrome using array

    843785
      hi, i made this codes to detect if string of array is palindrome, the code works fine but results a not correct, is detecting the word that a not palindrome as palindrome. Can anybody please tell me what i'm doing wrong public class PalindromeArrays
      {
      public static boolean isPalindrome(String cword)
      {
      int len = cword.length();
      char [] tempCharArray = new char[len];
      char [] charArray = new char[len];

      // put original string in an array of chars
      for (int i = 0; i<len; i++)
      {
      tempCharArray[i] = cword.charAt(i);
      }

      // reverse array of chars
      for (int j = 0; j < len; j++)
      {
      charArray[j] = tempCharArray[len - 1 - j];
      }

      String reversePalindrome = new String(charArray);

      if (cword.equals(reversePalindrome))
      return true;
      else
      return false;
      }
        • 1. Re: palindrome using array
          843785
          Could you post again using the code tags? (Highlight your code , then click the "CODE" button).

          Also it would be a good idea to fix the little typo (I think you have a missing } at the end) and, more importantly, include a main() method that illustrates what you are saying. That is, a test to show how your method produces "true" instead of false or vice versa.
          • 2. Re: palindrome using array
            843785
            Sorry i took time but i made some change still getting same answer
            may main mothod is
            import java.util.*;
            public class TestPalindrome {
            
                /**
                 * @param args the command line arguments
                 */
                public static void main(String[] args) 
                {
                    
                  Scanner keyboard = new Scanner(System.in);
                  String cword;
                  System.out.println("Enter a word:");
                  PalindromeArrays isPalindrome = new PalindromeArrays();
                  cword = keyboard.next();
                   if (true)
                   {
                       System.out.println("Is not palindrome");
                   } 
                   else
                   {
                    System.out.println("Is palindrome");  
                   }
                }
            
            }
            and my new palindrome array method is
            public class PalindromeArrays 
            {
                public static boolean isPalindrome(String cword)
                {
                    
                    char [] chars = cword.toCharArray();
                    
                    for (int i = 0, j = (chars.length-1); i < (chars.length/2); i++, j--)
                    {
                        if (chars[i] != chars[j])
                        {
                      
                          return false;
                        }
                    }
                   return true;
                }
                  
            }
            the main method and palindrome array are in the same package
            • 3. Re: palindrome using array
              843785
              Maybe your class and method for the palindrome array aren't detected.

              Also, you have an if statement to set it false if it isn't a palindrome, but the end of your method declares it true no matter what.
              • 4. Re: palindrome using array
                843785
                thank you i have seen i booleans. i guess the problem is detection, how to i make may main method to detect palindrome array
                • 5. Re: palindrome using array
                  843785
                  i meant to say how do i make the main method to detect
                  • 6. Re: palindrome using array
                    843785
                    Extend your main class to the sub-class, then call the Sub-class and method, end it with "();" as you would with any method.

                    Edited by: Sinanju180 on Dec 14, 2008 7:57 PM
                    • 7. Re: palindrome using array
                      843785
                      your code for finding a palindrome needs some work.
                      first, this is totally pointless:
                      for (int i = 0; i<len; i++)
                      {
                      tempCharArray[i] = cword.charAt(i);
                      }
                      String has a method that returns a char[] array called toCharArray().
                      anyway, creating 2 copies and reversing one isnt the optimal solution.
                      just iterate from the beginning of the string to the middle (length / 2)
                      and check if the char at that position is equal to the char on the
                      other side.
                      public class Example{
                      
                      public static void main(String[] args){
                           System.out.println(isPalindrome("racecar"));
                      }
                      
                      public static boolean isPalindrome(String word){
                      
                           for(int i = 0; i < (word.length() / 2); i++){
                           if(word.charAt(i) != word.charAt(word.length() - 1 - i)){
                           return false;
                           }
                           }
                      
                           return true;
                      
                      }
                      
                      }
                      • 8. Re: palindrome using array
                        843785
                        if u can see, i posted a second code, i made some correction, pos #3, remember i need to use array so i cant use ur codes, this is what i cant figure out (Extend may main class to the sub-class, then call the Sub-class and method) how to i do it
                        Is it like
                        PalindromeArrays isPalin = PalindromeArrays.isPalindrome();
                        • 9. Re: palindrome using array
                          843785
                          remember i need to use array so i cant use ur codes
                          Umm... You really have to say "I have to use array" in order for anyone to remember it.
                          PalindromeArrays isPalindrome = new PalindromeArrays();
                          cword = keyboard.next();
                          if (true)
                          {
                          // ...
                          In this code you never never actually call PalindromeArrays.isPalindrome().

                          Are you using your own code? I don't mean to be offensive, rather it's just that you have come up with two palindrome finding methods, both correct (I think), but both giving strange results because of how you invoke them. Perhaps you need to revise a few basics: how you invoke a static method either from a main() method in the class itself, or from some other class.
                          • 10. Re: palindrome using array
                            843785
                            @peace1: you might also want to consider the issue where the case of the letters are not the same in the initial string. For example:

                            Racecar...R != r

                            To avoid this, you can use the .toLowerCase() String method before creating the char array.

                            Luck,
                            CJSL
                            • 11. Re: palindrome using array
                              843785
                              Why not extend to to handles phrases and sentences:

                              A Toyota
                              Able was I ere I saw Elba.
                              Anal sex at noon taxes Lana.
                              O.J. nabs Ogopogo's banjo.