I am trying to make code with a GUI that allows the user to enter a word in the JTextField and then have it check if it's a palindrome (i.e Ono is a palindrome, it is the same back and forth). How would I take the JTextField and convert them into chars to check if the sentence is a palindrome? (and also, how would I remove the spaces, punctuation, etc?) This is my current code set up
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Stack;
/**
* Write a description of class Palidromania_Kevin here.
*
* @author Kevin Hy
* @version 09/23/13
*/
public class Palindromania extends JFrame
{
public JPanel panel1,panel2;
public JLabel main1;
public JButton check;
public JTextField enterPal;
public String inputWord,letter;
public int num;
public char checker;
public Stack<String> stack = new Stack<String>();
public Palindromania ()
{
super ("Palindromania Checker");
setSize (1024,768);
Container container = getContentPane();
panel1 = new JPanel();
panel1.setBackground (new Color(10,50,50));
panel1.setLayout (new FlowLayout());
panel2 = new JPanel();
panel2.setBackground (new Color(255,255,255));
panel2.setLayout (new FlowLayout());
main1 = new JLabel ("Palindromania Checker");
check = new JButton("Verify Palindrome");
ButtonHandler handler = new ButtonHandler();
check.addActionListener(handler);
enterPal = new JTextField(8);
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.CENTER);
panel2.add(enterPal);
panel2.add(check);
setVisible(true);
}
public static void main (String [] args)
{
Palindromania application = new Palindromania ();
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == check)//Check if palindrome button
{
inputWord="";letter="";
inputWord=enterPal.getText();
inputWord=inputWord.toLowerCase();
inputWord=inputWord.replace("[ !'-,]","");
for (int x=0; x<=inputWord.length()-1;++x)//inputs lettes into the stack
{
checker=inputWord.charAt(x);
letter+=checker;
stack.add(letter);
letter="";
JOptionPane.showMessageDialog (null, stack.peek());
}
for (int x=0; x<=inputWord.length()-1;++x)//inputs lettes into the stack
{
checker=inputWord.charAt(x);
letter+=checker;
stack.add(letter);
if (letter.equals(stack.pop()))
num+=1;
letter="";
}
if (num==inputWord.length())
JOptionPane.showMessageDialog (null, "You have a palindrome!");
else
JOptionPane.showMessageDialog (null, "This is not a palindrome, sorry!");
}
}
}
}EDIT: I modified back the code, made a mistake with the parse strings, and I can get the char letters to go in a test, and I can get the words that appear to go into the char and be outputted
EDIT2: So My main issue now, even though I can add stuff into the stack, is how come my .replace/.tolowercase on the string don't do anything? The strings I enter in the TextField do not get replaced (I.e HELLO WORLD stays as HELLO WORLD, not helloworld), and there is always a null in my stack, even though it is taking letters from inputWord? (i.e HELLO would be nullOLLEH) I also tried to put it into a string
compare+=stack.pop();
But all that does is it adds the letters with null to each? (nullnullO, nullOL nullOLL) Why does it have a null?
EDIT3: It works! A bit tedious the way I had to do it because I have to use a stack to compare (for the learning purposes)