hey, how's everyone,

i need help with a java program, i'll appreciate any help.

i'm need to write a program in java that will test if the input is a Palindrome or not.
i found a couple of programs, but they use a (do-while) loop which i'm not allowed to use.

i have to write it using stacks and queues and using LinkedList with an iterator

here's my program:

/
import java.util.*;

public class Palindrome
{
   public static void main(String[ ] args)
{
	Scanner palindrome = new Scanner(System.in); 
	String input;    
		{
	 System.out.print("Enter Input: ");
         input = palindrome.next();
	 if (isPalindrome(input))

	    System.out.println("That is a palindrome.");
	 else
	    System.out.println("That is not a palindrome.");
		}
      while (input.length() != 0); 
   }

   public static boolean isPalindrome(String input) 

   {   
      Queue<Character> queue = new ArrayDeque<Character>(); 
      Stack<Character> stack = new Stack<Character>(); 	  Character letter;
      int count = 0;
      int i;
      
      for (i = 0; i < input.length(); i++)
      {
	 letter = input.charAt(i);
         if (Character.isLetter(letter))
         {
            queue.add(letter);
            stack.push(letter);
         }
      }
      
      while (!queue.isEmpty()) 
      {
         if (queue.remove() != stack.pop()) 
            count++;
      }

  not then that represents how many characters didn't match.
      return (count == 0); 
   }
    
}

i'm told that my program goes into an infinite loop and that i shouldn't test the input to see if the characters are letters. javadoc comments say things that are not accurate.

The second program that uses LinkedList and Iterator:

/import java.util.*;

public class Palindrome
{

   public static void main(String[ ] args)
   {
	Scanner palindrome = new Scanner(System.in);
	String input;
	List<Character> LinkedList = new ArrayList<Character>();
		{
	 System.out.print("Enter Input: ");
         input = palindrome.next();
	 if (isPalindrome(input))
	 
	    System.out.println("That is a palindrome.");
	 else
	    System.out.println("That is not a palindrome.");
		}
   }

    public static boolean isPalindrome(String input)
   {
	List<Character> LinkedList = new ArrayList<Character>();
	int count = 0;
	int i;
	ListIterator begin = LinkedList.listIterator();
	ListIterator end = LinkedList.listIterator();
      for (i = 0; i < input.length(); i++)
		{
			LinkedList.add(input.charAt(i));
		}

      {
         if (!(begin.next() == end.previous()))
            count++;
      }

      return (count == 0); 
   }
    
}

here's the problems:

program will not run to completion, so it cannot produce correct output.

please help

thanx

Recommended Answers

All 7 Replies

abccba

if you add each letter in order to the stack and the queue, here is the final stack and queue (in the order they would be popped off)

1. abccba
2. abccba

But looking at something random that isn't a palindrome:

abc

1. cba (stack)
2. abc (queue)

So the idea is that if you add everything to the stack and the queue, then pop off one letter at a time... if the letters are always the same, you have a palindrome, otherwise, you don't. So the first step in your program should be to add everything to both the stack and the queue. And the second step should be to iterate through the lists, making sure the letter popped off the stack is the same as the letter popped off the queue... otherwise, you don't have a palindrome.

Stack is a LIFO data structure and a queue is a FIFO data structure. So a stack is similar to a pile of dishes (last one put on the pile is the first one you pick off the pile to wash) whereas a queue is similar to a line - first one in the line gets to go first - (and in fact, British people call what Americans call a line a queue)

so how do i do that, can you please give me an example.?

and is that why it goes into an infinite loop?!

if i shouldn't test the input to see if the characters are letters, what can i do in this case?!

thanx

If you use Java's built in Stack class and Queue class, you can simply call the appropriate methods to add the items on and take them off. For both of them, to add an item on, you probably just use the add method, for stack, to take it off, you probably use pop(), and for queue, it might be called next() or get() or something like that. Just google for the documentation and then read what the method descriptions say. It really is that simple. Although if you don't know what a stack or a queue is, I would recommend building your own. Of course, using the already existing classes is much faster. If you want to learn how to build your own stack and queue, I will help you implement the necessary methods. But if you want to use the easier, preexisting Java classes, then you're on your own because there is already plenty of documentation and examples on the web.

sorry, it's not going into my mind
i'm new to this :(

Er...do you even need to use all those data types??

private static boolean isPalindrome(String str)
{
       //end index 
       int end = str.length() - 1;
        
        //loop through string
        for(int index = 0; index < str.length();index++ )
        {
               //convert String to lower case..and if at anytime
               //the characters don't match return false
               if(str.toLowerCase().charAt(index) != 
                str.toLowerCase().charAt(end))
            {
                return false;
            }
            //decrement end index
            end--;
        }
        //looks like we got a palindrome!
        return true;
 }

thank you all for the help :)

i apritiate it

Er...do you even need to use all those data types??

Well, according to the OP, yes...

i have to write it using stacks and queues and using LinkedList with an iterator

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.