简体   繁体   English

我将如何接受TextField输入,接受每个字母并将其放入字符中?

[英]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 : 您的代码中有一些错误:

  1. 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中是不可变的。

  2. 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("[ !',-]","");

  3. 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就消失了。

  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. 最后,只需在堆栈上使用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.

相关问题 获取用户输入,放入数组并打印出每个字母的使用次数 - Take user input, put into an array and print out how many times each letter is used 我将如何接受用户输入并将其添加到arraylist中? - How would I take user input and add it into an arraylist? 如何输入并让程序在新行上打印出每个字母,同时遇到每个空格以打印出“ <space> “? - How do I take an input and make the program print out each letter on a new line while making every space encounter to print out “<space>”? 如何接受字母输入并使用if语句确定变量等于什么? - How to take a letter input and use an if statement to determine what a variable will be equal to? 我如何获取用户输入的数字并单独处理其数字? - How would I take a user-input number and work on its digits individually? 如何从一个班级的扫描仪中获取输入并将其放入另一班级 - How do I take an input from my scanner from one class and put it in another class 如何从扫描仪获取多个整数输入并将每个整数存储在单独的数组中? - How can I take multiple integer input from scanner and store each integer in separate arrays? 如何在 JDBC 中获取用户输入作为声明? - How to take take user input in JDBC for a Statement? 如何从输入中获取 2 个字母(忽略白色字符并且不能使用数组) - How to take 2 letters from input (ignoring white chars and can't use arrays) 如何递归获取输入字符串并返回每个字符重复的字符串? - How to recursively take an input string and return a string with each character repeated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM