[英]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 (ie Ono is a palindrome, it is the same back and forth). 我正在尝试使用GUI编写代码,该GUI允许用户在JTextField中输入单词,然后让它检查是否是回文(即Ono是回文,来回相同)。 How would I take the JTextField and convert them into chars to check if the sentence is a palindrome? 我将如何使用JTextField并将其转换为char以检查句子是否是回文? (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 }编辑:我修改了代码,在解析字符串中犯了一个错误,我可以让char字母进入测试,并且我可以得到看起来像进入char并输出的单词。
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? EDIT2:所以,即使我可以将内容添加到堆栈中,我现在的主要问题是如何在字符串上执行.replace / .tolowercase不执行任何操作? The strings I enter in the TextField do not get replaced (Ie 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? 我在TextField中输入的字符串不会被替换(即HELLO WORLD保持为HELLO WORLD,而不是helloworld),并且即使从inputWord接收字母,我的堆栈中始终存在null? (ie HELLO would be nullOLLEH) I also tried to put it into a string (即HELLO为nullOLLEH)我也尝试将其放入字符串中
compare+=stack.pop();
But all that does is it adds the letters with null to each? 但是,这是将每个字母都添加为null的字母吗? (nullnullO, nullOL nullOLL) Why does it have a null? (nullnullO,nullOL nullOLL)为什么它具有空值?
EDIT3: It works! EDIT3:有效! A bit tedious the way I had to do it because I have to use a stack to compare (for the learning purposes) 我这样做的方式有点乏味,因为我必须使用堆栈进行比较(出于学习目的)
There are a few mistakes in your code : 您的代码中有一些错误:
First your toUpperCase()
doesn't work because you need to assign the result of this call back to the variable. 首先,您的toUpperCase()
不起作用,因为您需要将此调用的结果分配回该变量。 So replace this : inputWord.toLowerCase();
因此,替换为: inputWord.toLowerCase();
by this : inputWord = inputWord.toLowerCase();
通过这样: inputWord = inputWord.toLowerCase();
. 。 This comes from String
being immutable in Java. 这是由于String
在Java中是不可变的。
You can also simplify your code by using a simple regex. 您还可以使用简单的正则表达式来简化代码。 Use inputWord.replace("[ !',-]","");
使用inputWord.replace("[ !',-]","");
instead of all your calls to replace and again assign the result to the variable. 而不是您所有的替换请求,而是再次将结果分配给该变量。 Like this : inputWord = inputWord.replace("[ !',-]","");
像这样: inputWord = inputWord.replace("[ !',-]","");
Now you have a null because you never initialize your letter
String. 现在您有了null,因为您从未初始化letter
String。 Just add a little letter=""
before your loop and the null should be gone. 只需在循环之前添加一个letter=""
,null就消失了。
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. 最后,只需在堆栈上使用pop()
即可在循环之后获取反转的字符串,并将其与原始字符串进行比较。 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); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.