File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes help!  trouble with pop method! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

help! trouble with pop method!

Russ Russ

Joined: Oct 21, 2002
Posts: 4
My applet is supposed to detect palindromes (words that are spelled the same forwards or backwords, ie "radar").
GUI is named WordGUI.
Stack class is named WordStack.
I am trying to use the pop() with stacks. I was able to use the push() without problems. Basically, I'm accepting user input from a textfield in my WordGUI class and pushing each char onto a stack array named data. Then I need to pop the chars out of the data array into a new stack array called popped. I then need to compare the contents of the 2 arrays to see if they are equal. If they are - we have detected a palindrome... mission accomplished.
So... what am I doing wrong with my pop method? I think there's also a problem with my for loop that uses pop. Any ideas? Thank you.
package assign4;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class WordGUI extends Applet implements ActionListener{
WordStack myStack = new WordStack();
private Button testButton;
private TextField wordField;
private boolean isPalindrome = false;
char popOutput;
public void init(){
wordField = new TextField(20);
testButton = new Button("Test for palindromes");
public void paint (Graphics g){
g.drawString("Enter a word in the textField above and click", 25, 100);
g.drawString("the button to check if the word is a palindrome", 25, 115);
myStack.display(g, 150);//used for testing purposes here
g.drawString("Is a palindrome!", 25, 75);
g.drawString("Is not a palindrome", 25, 75);
public void actionPerformed(ActionEvent event){
if(event.getSource() == testButton){
String userInput = wordField.getText();
isPalindrome = myStack.evaluate(userInput);
}//class WordGUI
package assign4;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class WordStack implements Cloneable{
private char[] data;
private char[] popped;
private int manyItems;
private boolean isPalindrome = false;
public WordStack(){
final int initialCapacity = 10;
manyItems = 0;
data = new char[initialCapacity];
popped = new char[initialCapacity];
}//generic constructor
public WordStack(int initialCapacity){
if(initialCapacity < 0){
throw new IllegalArgumentException
("initialCapacity too small: " + initialCapacity);
manyItems = 0;
data = new char[initialCapacity];
popped = new char[initialCapacity];
public int getCapacity(){
return data.length;
public boolean isEmpty(){
return (manyItems == 0);
public char pop(){
if(manyItems == 0){
//throw new EmptyStackException();
return data[--manyItems];

public boolean evaluate(String userInput){
for(int i = 0; i < userInput.length(); i++){
for(int i = 0; i < userInput.length(); i++){
popped[0] = pop();
return true;
return false;
public void push(char item){
if(manyItems == data.length){
ensureCapacity(manyItems * 2 + 1);
data[manyItems] = item;
public void ensureCapacity(int minimumCapacity){
char biggerArray[];
if(data.length < minimumCapacity){
biggerArray = new char[minimumCapacity];
System.arraycopy(data, 0, biggerArray, 0, manyItems);
data = biggerArray;

public void display(Graphics g, int yLoc){//being used for testing purposes here
for(int i = 0; i < data.length; i++){
g.drawString("" + data[i] + popped[i], 25, yLoc);
yLoc += 15;
public void trimToSize(){
char trimmedArray[];
if(data.length != manyItems){
trimmedArray = new char[manyItems];
System.arraycopy(data, 0, trimmedArray, 0, manyItems);
data = trimmedArray;
}//class WordStack
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Please do not cross post.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: help! trouble with pop method!
It's not a secret anymore!