Search code examples
javaswingcharstackjtextfield

How would I take a TextField input, take each letter, and put it into chars?


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)


Solution

  • There are a few mistakes in your code :

    1. First your toUpperCase() doesn't work because you need to assign the result of this call back to the variable. So replace this : inputWord.toLowerCase(); by this : inputWord = inputWord.toLowerCase();. This comes from String being immutable in Java.

    2. You can also simplify your code by using a simple regex. Use inputWord.replace("[ !',-]",""); instead of all your calls to replace and again assign the result to the variable. Like this : inputWord = inputWord.replace("[ !',-]","");

    3. Now you have a null because you never initialize your letter String. Just add a little letter="" before your loop and the null should be gone.

    4. And at the end just use pop() on the stack to get the reversed string just after the loop and compare it to the original one. Just like this :

      if(inputWord.equalsIgnoreCase(stack.pop())){
             JOptionPane.showConfirmDialog(null, "This is a Palindrome", "Is this a palindrome ?", JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE);
      }
      else {
             JOptionPane.showConfirmDialog(null, "This is NOT a Palindrome", "Is this a palindrome ?", JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE);
      }