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. <code> 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); add(wordField); wordField.addActionListener(this); testButton = new Button("Test for palindromes"); add(testButton); testButton.addActionListener(this); }//init() 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.trimToSize(); myStack.display(g, 150);//used for testing purposes here if(isPalindrome){ g.drawString("Is a palindrome!", 25, 75); } else{ g.drawString("Is not a palindrome", 25, 75); } }//paint() public void actionPerformed(ActionEvent event){ if(event.getSource() == testButton){ String userInput = wordField.getText(); isPalindrome = myStack.evaluate(userInput); } repaint(); }//actionPerformed() }//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]; }//constructor public int getCapacity(){ return data.length; }//getCapacity() public boolean isEmpty(){ return (manyItems == 0); }//isEmpty() public char pop(){ if(manyItems == 0){ //throw new EmptyStackException(); } return data[--manyItems]; }//pop()
public boolean evaluate(String userInput){ for(int i = 0; i < userInput.length(); i++){ push(userInput.charAt(i)); } for(int i = 0; i < userInput.length(); i++){ popped[0] = pop(); } if(data[].equals(popped[])){ return true; } else{ return false; } }//evaluate() public void push(char item){ if(manyItems == data.length){ ensureCapacity(manyItems * 2 + 1); } data[manyItems] = item; manyItems++; }//push() public void ensureCapacity(int minimumCapacity){ char biggerArray[]; if(data.length < minimumCapacity){ biggerArray = new char[minimumCapacity]; System.arraycopy(data, 0, biggerArray, 0, manyItems); data = biggerArray; } }//ensureCapacity()
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; } }//display() public void trimToSize(){ char trimmedArray[]; if(data.length != manyItems){ trimmedArray = new char[manyItems]; System.arraycopy(data, 0, trimmedArray, 0, manyItems); data = trimmedArray; } }//trimToSize() }//class WordStack </code>