Search code examples
javastackpalindrome

Issue with Palindrome class using Stacks


I am trying to write a Palindrome class using Stacks to determine if a word entered by user is a palindrome. There seems to be an issue in my Palindrome class. Can someone please help me identify it? My program works but no matter what word I type, it returns that the word is not a palindrome.

import java.util.Stack;
public class Palindrome
{
     public Palindrome()
     {
        Stack stack = new Stack();
        String input = "";
        boolean isPalindrome = false;
        for(int i = 0; i < input.length(); i++)
        {
            stack.push(i);
        }

        String opposite = " ";
        while(!stack.isEmpty())
        {
            opposite = opposite + stack.pop();
        }

        if(input.equals(opposite))
            isPalindrome = true;
        else
            isPalindrome  = false;
    }//end main   
}//end class Palindrome 

Here is my PalindromeCheck class:

import java.util.Scanner;
public class PalinDromeCheck
{
   public static void main(String[] args)
   {
       Palindrome pal = new Palindrome();
       Scanner type = new Scanner(System.in);       //create scanner for  user to type a word

       String word = null;                          //initial word to check
       String stop = null;                          //stops processing of word

       do
       {
           System.out.println("Enter a word to determine if it is a palindrome: ");     
           word = type.nextLine();                  //user types word

           if(pal.equals(word))                 
               System.out.println("is a palindrome");
           else
               System.out.println("is not a palindrome\n");

           System.out.println("Would you like to try another word? Type Y for 'Yes' or N for 'No'");
           stop = type.nextLine();                  //stops processing
       }
       while(stop.equalsIgnoreCase("y"));           //continues to process and ignores upper or lowercase Y
   }
}

Solution

  • You have several bugs.

    • First, you need to give the input to the Palindrome class.
    • Second, when you reverse the input using stack, you push the index on the stack, not the character.
    • Third, it's not a good practice to do everything une constructor. Palindrome class doesn't need to know the input as member or for initialization purpose

    If you don't need to have several implementation of Palindrome, you shall use a static method.

    Try this :

    public class Palindrome
    {
        public static boolean isPalindrome(String input)
         {
    
             char[] inputArray = input.toCharArray();
             bool isOk = true;
             for(int i = 0; i < inputArray.length/2 && isOk; i++){
                 isOk &= inputArray[i] == inputArray[inputArray.length - i - 1];
             }
             return isOk;
         } // end method
    } //end class Palindrome 
    

    Then, your main function could be :

    public static void main(String[] args)
    {
        System.out.println("Enter a word to determine if it is a palindrome: ");     
        word = type.nextLine();                  //user types word
    
        if(Palindrome.isPalindrome(word)) {        
            System.out.println("is a palindrome");
        } else {
            System.out.println("is not a palindrome\n");
        }
    } // End of main