Hi I have a homework assignment that is due today. I've been working on it all week, but I just can't get it. My assignment is to write a program that creates a stack and tells if its a palindrome. Here's what I have:

import javax.swing.JButton;
import javax.swing.JFrame;
import java.util.Stack;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class Palindromes extends JFrame
{
public static void main(String[] args)
{
do
{
System.out.print("Enter a sentence: ");
String sentence = SavitchIn.readLine();
if (isPalindrome(sentence))
System.out.println("That is a palindrome.");
else
System.out.println("That is not a palindrome.");
} while (keepGoing());
}


private static boolean isPalindrome(String s)
{
String simple = "";
String reverse = ""; // will hold the letters of s in reverse
char c;
s = s.toLowerCase();
for (int i = 0; i < s.length(); ++i)
{
c = s.charAt(i);


if ((c >= 'a') && (c <= 'z'))
{
}
simple = simple + c;
reverse = c + reverse;
}
return (simple.equals(reverse));
}


private static boolean keepGoing()
{
System.out.print("Enter another sentence? [y/n] ");
char answer = SavitchIn.readLineNonwhiteChar();
return ((answer == 'y') || (answer == 'Y'));
}


public void Stack()
{
count = 0;
capacity = 10;
capacityIncr = 5;
while (phrase.length() != s.size() );
{
String oneChar;
onfiltered= phrase.substring(n-1, n);
s.push(oneChar);
n++;
}
while(s.size()>0);
reversePhrase = reversePhrase + s.pop();
}



public Palindromes()
{
//Create the input text box
final TextField input = new TextField("");


// Create the input area and place in the NORTH area.
Panel inputPanel = new Panel(new GridLayout(2,3));
inputPanel.add(new Label("")); //position 1
inputPanel.add(new Label("")); //position 2
inputPanel.add(new Label("")); //position 3
inputPanel.add(new Label("Please enter a word:", Label.RIGHT)); //position 4
inputPanel.add(input); //position 5
inputPanel.add(new Label("")); //position 6
add(inputPanel, BorderLayout.NORTH);



// Create a label for output
final Label output = new Label("", Label.CENTER);


// Create the output area and place it in the CENTER area.
Panel outputPanel = new Panel(new GridLayout(2,1));
outputPanel.add(new Label("")); //position 1
outputPanel.add(output); //position 2
add(outputPanel, BorderLayout.CENTER);



// Create a button and add a listener.
final Button palButton = new Button("Is A Palindrome?");
palButton.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {


if (palButton.getActionCommand() == "Is A Palindrome?")
{
String original = input.getText();
boolean isPal = Palindrome(original);


if (isPal)
{
output.setText("The word: " + input.getText() +
" , is a palindrome!");


palButton.setLabel("Clear Word");
}
else
{
output.setText("The word: " + input.getText() +
" , is not a palindrome!");
input.setText("");
input.requestFocus();
}
}
else if (palButton.getActionCommand() == "Clear Word")
{
output.setText("");
input.setText("");
input.requestFocus();
palButton.setLabel("Is A Palindrome?");
}
}
});



// Create the button area and place SOUTH area.
Panel buttonPanel = new Panel(new GridLayout(3, 3));
buttonPanel.add(new Label("")); //position 1
buttonPanel.add(new Label("")); //position 2
buttonPanel.add(new Label("")); //position 3
buttonPanel.add(new Label("")); //position 4
buttonPanel.add(palButton, "2,2"); //position 5
buttonPanel.add(new Label("")); //position 6
buttonPanel.add(new Label("")); //position 7
buttonPanel.add(new Label("")); //position 8
buttonPanel.add(new Label("")); //position 9
add(buttonPanel, BorderLayout.SOUTH);



// Set frame properties
setTitle("Lab5 - Using the Stack");
setSize(new Dimension(600, 300));
setLocation(350, 225);
show();
allowClosing();


}


/**
* This method listens for a WindowEvent requesting to close the application
*/
public void allowClosing()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
});
}


/**
* This method calls a new PalindromeTest application
*/
public static void main(String[] args)
{


new Palindromes();


}
}

Edited 3 Years Ago by happygeek: fixed formatting

What's it not doing? There's a chance it wont even show up because you extend a swing component, but yet you are using awt components? (ie: Panel, TextField, etc)... I've seen cases were it won't display because of this.

What's it not doing? There's a chance it wont even show up because you extend a swing component, but yet you are using awt components? (ie: Panel, TextField, etc)... I've seen cases were it won't display because of this.

thanks,what should I do. My Stack keeps giving me an expected error.

<identifier> expected. this is what I have written:

private Stack<Character> charStack = new Stack<Character>();

Compiling that code yields something like a hundred compiler errors.
Start at the top of the list and resolve them one by one.

This article has been dead for over six months. Start a new discussion instead.