简体   繁体   English

从 JTextArea 将文本存储为 arraylist

[英]Storing text as an arraylist from JTextArea

I need to create a program to store all words in an array list.我需要创建一个程序来将所有单词存储在一个数组列表中。 Then check the user input from the textfield to see if it starts with anything other than numbers and punctuation.然后检查文本字段中的用户输入,看看它是否以数字和标点符号以外的任何内容开头。 Otherwise it will need to display an error and prvent the string to be added to the arraylist and display an appropriate error.否则将需要显示错误并防止将字符串添加到 arraylist 并显示适当的错误。

https://pastebin.com/8UwDm4nE https://pastebin.com/8UwDm4nE

Heres the ActionEvent listener that contins the code to check that.下面是 ActionEvent 侦听器,它继续检查该代码。 Im not really sure how to get it working.我不确定如何让它工作。

@Override
public void actionPerformed(ActionEvent e) {
    for(int i = 0; i < 1; i++) {
        String str = tf.getText(); // MUST BE STORED ON AN ARRAY LIST
        ta.append(str + "\n"); // Append the text on new line each

        if(str.startsWith(String.valueOf(nums))) { // Check input for a number at the start
            error.setText("Error: Word starts a number. Please try again!");
            error.setForeground(Color.RED);
            ta.append("");
        } else if (str.startsWith(String.valueOf(punct))) { // Check if input contains a punctuation at the start
            error.setText("Error: Word starts with an illegal character. Please try again!");
            error.setForeground(Color.RED);
            ta.append("");
        }
    }
}

I'm going to rephrase your problem a bit as clarification, please correct me if I'm misunderstanding.我将把您的问题重新表述为澄清,如果我有误解,请纠正我。

You have a text field and a text area.您有一个文本字段和一个文本区域。 You want a user to type a word into the text field and submit it.您希望用户在文本字段中输入一个单词并提交它。 If that word starts with a number or punctuation, then indicate an error to the user.如果该单词以数字或标点符号开头,则向用户指示错误。 Otherwise, add it to the text area (on a new line) and the inner ArrayList.否则,将其添加到文本区域(在新行上)和内部 ArrayList。

To solve this problem, there are a couple things you'll need:要解决此问题,您需要做几件事:

  1. An ArrayList<String> that is a class member variable where you can store your words一个ArrayList<String> ,它是一个 class 成员变量,您可以在其中存储您的单词
  2. An event handler that handles the button click.处理按钮单击的事件处理程序。

The event handler should:事件处理程序应该:

  1. Parse the string from the text field (using getText() , as you already are).解析文本字段中的字符串(使用getText() ,就像您已经使用的那样)。
  2. Do the error checks you're already doing.做你已经在做的错误检查。
  3. If neither of the error conditions are hit (so add an else clause for this), add the word to the text area (which you're already doing) and add it to the ArrayList.如果没有遇到任何错误条件(因此为此添加else子句),则将单词添加到文本区域(您已经在执行此操作)并将其添加到 ArrayList。

Hopefully this helps you get a clearer idea of how to approach the problem.希望这可以帮助您更清楚地了解如何解决问题。 If not, please post a code sample of what you tried and what error you're specifically running into.如果没有,请发布您尝试过的代码示例以及您具体遇到的错误。

EDIT: Here is some pseudocode for your if-else error-handling block of code, assuming you declare a new ArrayList to hold your words as a class member:编辑:这是您的 if-else 错误处理代码块的一些伪代码,假设您声明了一个新的ArrayList以将您的话保存为 class 成员:

// as class member variable
List<String> wordList = new ArrayList<>();

// word handler code
if (str starts with a number) {
    // handle error
} else if (str starts with punctuation) {
    // handle error
} else {
    ta.append(str + "\n");
    wordList.add(str);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM