Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would I take a TextField input, take each letter, and put it into chars?
    primarykey
    data
    text
    <p>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</p> <pre><code>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&lt;String&gt; stack = new Stack&lt;String&gt;(); 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&lt;=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&lt;=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!"); } } } </code></pre> <p>}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</p> <p>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</p> <pre><code>compare+=stack.pop(); </code></pre> <p>But all that does is it adds the letters with null to each? (nullnullO, nullOL nullOLL) Why does it have a null?</p> <p>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)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload