2 Replies - 6300 Views - Last Post: 28 January 2011 - 03:18 PM Rate Topic: -----

#1 cyberbemon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 10

palindrome (stack & queue) ! >.<

Posted 28 January 2011 - 12:36 PM

hello guys i have to write a program that takes in a user input and then puts into a stack and queue then i have to remove and compare them to see if they are a palindrome.here is what i have done..when i enter a word that is a palindrome it doesn't print saying so instead it prints out saying its not a palindrome.(its something to do with my loop but i'm lost :( )i'm only including the test file.(i was given the other stack interface,stack,queue interface,queue files and was asked to use them to write this)

if someone could point out the mistake i'd be gratefull :)

import javax.swing.JOptionPane;


public class testpalindrome {
	public static void main(String[] args)
	{
		int x=0;
		Stack s = new ArrayStack();
		Queue q = new ArrayQueue();
		String orig = JOptionPane.showInputDialog("enter the string").toUpperCase().replace(" ", "" );
	
		if(orig.length()==0)//if its a null string
		{
			JOptionPane.showMessageDialog(null,"Try again and please enter a valid string");
			return;
		  
		}
		else 
		{ 
			for(int i=0;i<orig.length();i++)
			{
				Character temp = new Character(orig.charAt(i));
				
				q.enqueue(temp);
				s.push(temp);
			}
		}
		while(!q.isEmpty())
		{	
		
			if(q.dequeue()!=s.pop())
			{
				System.out.print("not a palindrome");
				break;
			}
			else 
			{
				++x;
				continue;	
			}
		}
		
		if (x==orig.length())
		{
			System.out.print("it is a palindrome");
		}
			
	}
	

}



Is This A Good Question/Topic? 0
  • +

Replies To: palindrome (stack & queue) ! >.<

#2 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: palindrome (stack & queue) ! >.<

Posted 28 January 2011 - 12:53 PM

When comparing classes use .equals instead of == for comparison, otherwise you're checking memory addresses.
if(q.dequeue().equals(s.pop()))


Also, this is completely unnecessary, you're just wasting memory:
Character temp = new Character(orig.charAt(i));


Just do:
Character temp = orig.charAt(i);


Yours,
Shane~

This post has been edited by ShaneK: 28 January 2011 - 12:53 PM

Was This Post Helpful? 1
  • +
  • -

#3 cyberbemon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 14-October 10

Re: palindrome (stack & queue) ! >.<

Posted 28 January 2011 - 03:18 PM

View PostShaneK, on 28 January 2011 - 12:53 PM, said:

When comparing classes use .equals instead of == for comparison, otherwise you're checking memory addresses.
if(q.dequeue().equals(s.pop()))


Also, this is completely unnecessary, you're just wasting memory:
Character temp = new Character(orig.charAt(i));


Just do:
Character temp = orig.charAt(i);


Yours,
Shane~


thank you soo much shane,the program works now ^_^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1