Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

palindrome Tester...

800419Feb 10 2009 — edited Feb 13 2009
I need a bit of help making a palindrome tester...

For those of you unfamiliar w/ palindromes its a word like so:

Raceacr

spelled the same way forward and backward...

so here is my code:
import java.util.*;
public class Palindrome
{
    
     public static void main(String[] args)
     {
         
         
         
         String deLimiters = ";:'<>,./?-+!@#$%^&*() ";
         Stack<String> stack = new Stack<String>();
         Scanner reader = new Scanner(System.in);
         
         while(true)
         {
            String palindrome = "";
            String backwards = "";
            
            System.out.println("Please enter a palindrome (Type n to quit): ");
            String str = reader.nextLine();
            
            if(str.equalsIgnoreCase("n"))
            {
                System.exit(0);
            }
         
         
            StringTokenizer tokens = new StringTokenizer(str, deLimiters);
            StringTokenizer tokens2 = new StringTokenizer(palindrome, deLimiters);
         
            
            while(tokens.hasMoreTokens())
            {
                 palindrome += tokens.nextToken();
            }
          
        
            for(int i = 0; i < palindrome.length(); i++)
            {
                 stack.add(palindrome.substring(i, i +1));   
            }
         
         
            while(! stack.empty())
            {
                backwards += stack.pop();
            }
        
       
            if(palindrome.equalsIgnoreCase(backwards))
            {
                System.out.println("Your phrase: " + palindrome + " is a Palindrome");
            }
            else
            {
                System.out.println("Your phrase: " + palindrome + " is not a Palindrome");
            }
        }
        
    }
}
i need some help just making it work...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 13 2009
Added on Feb 10 2009
17 comments
153 views