0

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)

Bobby Ores
  • 93
  • 6
  • 3
    Why are you pushing the string onto a stack? If you know the gist of what you want to do, then explain it to us. This *seems* to be a request for someone to write the code for you, which is not considered appropriate. Show what you have done so far to solve the specific problem you're having. – Jim Garrison Sep 24 '13 at 18:20
  • 2
    Why not simply [reverse the `String`](http://stackoverflow.com/a/7569370/1076463) and compare it with the original one – Robin Sep 24 '13 at 19:43
  • I can't reverse the string because we are supposed to use a stack or queue to compare the strings together (since the stack takes the letters in and the word itself would be backwords, it should be able to compare?) – Bobby Ores Sep 25 '13 at 12:18

1 Answers1

1

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);
    }
    
Marc
  • 2,631
  • 1
  • 12
  • 13
  • Thanks for the help! Although I couldn't use the word comparing, because I have to compare each individual letter (as a learning process of using stacks, individual letters and all). My code evolved into 2 of the almost the same for loops to compare each individual letter – Bobby Ores Sep 25 '13 at 13:29
  • It all works, but I have one last issue, it's that if I use a palindrome that has punctuation before hand or after, it is not a palindrome? (i.e Rats live on no evil star, if I put ! before rats or after star, it doesn't work?) – Bobby Ores Sep 25 '13 at 13:39
  • So you don't want to remove punctuation at all ? – Marc Sep 25 '13 at 13:47
  • I want to remove the punctuation, but when I place (for example, Rats live on no evil star is a palindrome) but when I put a punctuation, say ! in front of Rats or after star, it doesn't remove the punctuation and doesn't register it as a palindrome? (!rats live on no evil star is not a palindrome, although it should remove it) – Bobby Ores Sep 26 '13 at 17:36