简体   繁体   English

比较输入和对象数组列表

[英]compare input to an object arraylist

I would like to compare the input from a JTextField to all the elements in a string arraylist. 我想将JTextField的输入与字符串arraylist中的所有元素进行比较。 If the input is equal to an element in the list I would like the program to acknowledge by saying "This is in my vocabulary.", and if it is not, I would like the program to say "This is NOT in my vocabulary." 如果输入等于列表中的元素,我希望程序通过说“这在我的词汇表中”来确认,如果不是,我希望程序说“这不在我的词汇表中”。 ” In my code, I have tried getting this to work buy I always get the message "This is NOT in my vocabulary." 在我的代码中,我尝试使它正常工作,但我总是收到消息“这不在我的词汇表中”。 even if the input matches an element in my list. 即使输入匹配我列表中的元素。 How can I get this to work properly? 我该如何使其正常工作?

Here is my code, in it AI is where the list that is being compared is. 这是我的代码,其中AI是要比较的列表所在的位置。

package Important;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import AI.*;

public class TextActions implements ActionListener{

private String hero;
private Vocabulary vocab1 = new Vocabulary();

public void actionPerformed(ActionEvent e) {

    e.getSource();
    hero = e.getActionCommand();

    if (vocab1.Adjectives.equals(hero)){
        System.out.println("This word is in my vocab");
    }else{
        System.out.println( hero + " is not in my vocab");
    }
    //CompareWords(hero);
}

public void CompareWords(String readme){

    if (vocab1.Adjectives.contains(readme)){
        //System.out.println("This word is in my vocab");
    }
}

}

Here is the Vocabulary class as requested. 这是所要求的词汇课。

package AI;

import java.util.*; 导入java.util。*;

public class Vocabulary { 公共类词汇{

//String[] thoughts;
public List<String> Adjectives = new ArrayList<String>();


public void AddWord(int ArrayListNumber, String WordEntered){

    if(ArrayListNumber == 1){
    Adjectives.add(WordEntered);
    }
}

} }

You are creating new object 您正在创建新对象

Vocabulary vocab1 = new Vocabulary();

This would contain an empty Adjectives list. 这将包含一个空的形容词列表。 So, you have to first populate the Adjectives list before you do the checking as follows 因此,您必须先填充形容词列表,然后按照以下步骤进行检查

vocab1.Adjectives.contains(hero) 

and use contains instead of equals. 并使用包含而不是等于。

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

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