I'm working on a Palindrome program. For some reason, my compare method always return zero. "c" should suppose to increment 1 when both letter are the same, but my method would not increment 1 when the letter are the same for some reason. I'm not sure where is the problem.

This is the method I create to compare:

public int compare(String words)
	{
		ArrayQueue<Character> q = new ArrayQueue<Character>();
		Stack<Character> s = new Stack<Character>();
		int c=0;

		
		for(int i =0; words.length()<i; i++)
		{
			q.enqueue(words.charAt(i));
			s.push(words.charAt(i));
		}
		
		while(!s.isEmpty())
		{
			if (s.pop().equals( q.dequeue() ) )
				c++;
			
		}		 
		
		if(c==0)
			return 0;
		else
			return 1;
	}

it is always return 0, I have been think and search for error for over two hours. I still don't get why it would always return 0;

Recommended Answers

All 6 Replies

well ... I'm not sure why you make a palindrome check this complicated, but anyway..
I think either you didn't think your logic out straight, or you 're mistaken about what a palindrome is.

also: what ArrayQueue class are you using?

all you really needed to do is:
iterate over the chars of the String
a bit like this =>

int counter = 0
do (word.length/2 times){
if ( word.charAt(counter) != word.charAt(length-counter))
return false

}

// you only get here if it is a palindrome
return true

Sorry, I just modify it two minutes ago. I hope this is much easier to read

This is compare class

import java.util.Stack;

public class PalindromeCompare 
{	
	public boolean compare(String words)
	{
		ArrayQueue<Character> q = new ArrayQueue<Character>();
		Stack<Character> s = new Stack<Character>();
		boolean c=false;
		char k;

		
		for(int i =0; i > words.length(); i++)
		{
			k = words.charAt(i);
			if(Character.isLetter(k) )
			{
				q.enqueue(k);
				s.push(k);
			}
		}
		
		while( !q.isEmpty())
		{
			if (s.pop().equals( q.dequeue() ) )
			{
				 c = true;
			}
			else
			{
				 c= false;
				 break;
			}
		}
		return c;

	}
}

This is the testing class:

import java.util.Scanner;

public class PalindromeTest
{
	public static void main(String[] args)
	{
		PalindromeCompare p = new PalindromeCompare();
		Scanner input = new Scanner(System.in);
		
		System.out.println("Please enter the words: ");
		String sr = input.nextLine();
		sr.replace(" ","" );
		sr.toLowerCase();
		System.out.println(p.compare(sr));
		if(p.compare(sr))
		{
			System.out.println("This is a Palindrome");
		}
		else
			System.out.println("This is not a palindrome");
		
	}
}

This is the queue impliment array

import java.lang.IllegalArgumentException;

/**
 * Implement the Queue ADT as a circular queue using an array.
 */
public class ArrayQueue<E> implements Queue<E> {
  private E[] queue;
  private int capacity;
  private int size;
  private int front;
  private int rear;

  public ArrayQueue() {
    this( DEFAULT_CAPACITY );
  }

  public ArrayQueue( int maxElements ) {
    if ( maxElements <= 0 ) {
      throw new IllegalArgumentException();
    }
    this.queue = ( E[] )new Object[maxElements];
    this.capacity = maxElements;
    this.size = 0;
    this.front = 0;
    this.rear = 0;
  }

  public int capacity() {
    return this.capacity;
  }

  public void clear() {
    // empty the queue, making all the array cells null
    while ( this.size != 0 ) {
      dequeue();
    }
    this.front = 0;
    this.rear = 0;
  }

  public E dequeue() {
    if ( this.isEmpty() ) {
      throw new EmptyQueueException( "The queue is empty" );
    }
    E element = this.queue[front];
    this.queue[this.front] = null;
    this.front = ( this.front + 1 ) % this.capacity;
    this.size--;
    return element;
  }

  public void enqueue( E element ) {
    if ( this.isFull() ) {
      throw new FullQueueException( "The queue is full" );
    }
    this.queue[this.rear] = element;
    this.rear = ( this.rear + 1 ) % this.capacity;
    this.size++;
  }

  public E peek() {
    if ( this.isEmpty() ) {
      throw new EmptyQueueException( "The queue is empty" );
    }
    return this.queue[this.front];
  }

  public boolean isEmpty() {
    return this.size == 0;
  }

  public boolean isFull() {
    return this.size == this.capacity;
  }

  public int size() {
    return this.size;
  }
}

Thank you so much. I'm so sorry I make it so complicated.

wow ... all that to replace about four lines of code? :D
I'll give it a look.

Sorry, I forgot to mention one things about this project. I need to use queue and stack to compare according the specification. Another question about this line

do (word.length/2 times){

because it would give me a syntax errors for do(words.length/2). The last question, do you mean replace all the code in PalindromeCompare to with those four lines or just the while(!q.isEmpty()) part?

it was pseudo code, not actual java code...

well, basically ... those four lines is all you need to check whether or not a word is a palindrome or not.
so, no need for anything except them.

I tried your code, but I don't seem to have all the classes and exceptions you're using in there.

have you tried adding prints in your code to check whether it is returning what you think it is returning?

I'm sorry. a few more class need to post

/**
 * Interface for the Queue ADT with a fixed upper bound on
 * the number of elements that can be stored in the queue.
 */
public interface Queue<E> extends java.io.Serializable {

  /**
   * The default number of entries in a Queue.
   */
  static final int DEFAULT_CAPACITY = 100;

  /**
   * Return the upper bound on the number of elements this
   * Queue can store.
   * @return the capacity of this queue.
   */
  public int capacity();

  /**
   *  Empty the queue of all elements.
   */
  public void clear();

  /**
   * Add <tt>element</tt> to the end of the queue.
   * @param element The element to add to the rear of the queue
   * @throws FullQueueException if the queue is full
   */
  public void enqueue( E element );

  /**
   * Remove and return the element at the front of the queue.
   * @return this queue's front element
   * @throws EmptyQueueException if the queue is empty
   */
  public E dequeue();

  /**
   * Determine if this queue has any elements.
   * @return <tt>true</tt> if this queue has  no elements
   *     (<tt>size() == 0</tt>); <tt>false</tt> otherwise.
   */
  public boolean isEmpty();

  /**
   * Determine if this queue has room for more elements.
   * @return <tt>true</tt> if this queue has room for more
   *   elements (<tt>size() == capacity()</tt>);
   *   <tt>false</tt> otherwise.
   */
  public boolean isFull();

  /**
   * Return the element at the front of this queue. This
   * operation does not change the state of this queue.
   * @return the element at the front of this queue
   * @throws EmptyQueueException if the queue is empty
   */
  public E peek();

  /**
   * Determine the number of elements stored in this queue.
   * @return the number of elements in this queue
   */
  public int size();
}

exception

public class FullQueueException extends RuntimeException {

  public FullQueueException() {
    super();
  }

  public FullQueueException( String errMsg ) {
    super( " " + errMsg );
  }
}

Another one

/**
 * Thrown when there is an attempt to access the front
 * of an empty queue.
 */
public class EmptyQueueException extends RuntimeException {

  public EmptyQueueException() {
    super();
  }

  public EmptyQueueException( String errMsg ) {
    super( " " + errMsg );
  }
}

Well, cause my teacher mention about using pop and dequeue to do it so that is reason why I used it. Your method probably would work, but I still need to follow the specification and I would like know why my code is not working. I know it is a logic error.

Yes, I did test by print what is it return . It is always returning the default value which in my case would be false no matter what even the senetence is plindrome.

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.