Java Hashtable containsValue() 不起作用

标签 java string text hashtable

我正在尝试使用字典单词的哈希表编写拼写检查器。我目前正在尝试将文本文档中的单词与哈希表中的值进行比较,以查看其拼写是否正确,但无论我做什么, containsValue() 方法总是返回 false,即使我知道它不是。

public class SpellChecker {

    private Hashtable<Integer, Wrapper> words;
    private ArrayList<Wrapper> parsedFile;

    SpellChecker() {
        words = new Hashtable<Integer, Wrapper>();
        parsedFile = new ArrayList<Wrapper>();
    }

    public void createDict(String inputFile) throws IOException {
        FileReader input = new FileReader(inputFile);
        BufferedReader bufRead = new BufferedReader(input);
        String myLine = null;
        int i = 0;
        while ((myLine = bufRead.readLine()) != null)
        {   
            Wrapper my_line = new Wrapper(myLine);
            words.put(i,my_line);
            i++;
        }
        bufRead.close();
    }

    public ArrayList<Wrapper> readFile(String inputFile) throws IOException {
        FileReader input = new FileReader(inputFile);
        BufferedReader bufRead = new BufferedReader(input);
        String myLine = null;
        Wrapper[] array_file;

        while ((myLine = bufRead.readLine()) != null)
        {   
            String[] arrayFile = myLine.split(" ");
            for (int i = 0; i < arrayFile.length; i++) {
                array_file = new Wrapper[arrayFile.length];
                arrayFile[i] = arrayFile[i].toLowerCase();

                Wrapper my_line = new Wrapper(arrayFile[i]);
                array_file[i] = my_line;
                parsedFile.add(array_file[i]);
            }
        }

        bufRead.close();
        return parsedFile;
    }

    public ArrayList<Wrapper> getParsedFile(){
        return parsedFile;
    }

    public ArrayList<String> checkMisspellings() {
        ArrayList<String> misspelled = new ArrayList<String>();

        for (int i = 0; i<parsedFile.size(); i++) {
            if (!words.containsValue(parsedFile.get(i))) {
                misspelled.add(parsedFile.get(i).backToString());
            }
        }
        return misspelled;
    }
}

我在网上查看了一些答案,说这可能是因为 containsValue() 比较地址,所以我为 String 值创建了一个包装类,但它仍然不起作用。

public class Wrapper {

    public String x;

    public Wrapper(String x){
        this.x=x;
    }

    public String backToString() {
        return x;
    }

    public boolean equals(Object o) {
   if(o == null) return false;
   if(!(o instanceof Wrapper)) return false;
   final Wrapper p = (Wrapper) o;
   if(p.x.equals(this.x)) return true;
   return false;
}

    @Override
    public int hashCode() {
      int hash = 3;
      hash = 53 * hash + x.length();
      return hash;
    }
}

拼写错误的 arrayList 应该只包含哈希表中找不到的单词,但它总是只返回所有原始单词。我究竟做错了什么?? 以下是一些示例输入/输出:

input: hellos my name Ann corral mlks please spell correct 
output: [��hellos, my, name, ann, corral, mlks, please, spell, correct, ]

我用于字典的文本文件如下所示:

corralled
corralling
corrals
correct
correctable
corrected
correcter
correctest
correcting

最佳答案

equals() 方法中将 p.x==this.x 更改为 p.x.equals(this.x)

如需了解更多信息,请了解how to compare Strings in Java .

关于Java Hashtable containsValue() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54264798/

相关文章:

java - 如何将观察者模式与 jms 接收器一起应用

string - 生成特定格式的唯一字符串的算法

c++ - std::string 是一个对象吗?

javascript - 如何将文本转换为输入?

java - 日志记录在 Stripes 框架应用程序中不起作用

java - 将 PHP 代码连接到 Java 后端

java - 将数组的个数相加直到达到某个值

javascript - 在替换之前获取子字符串的位置

python - 使用 Python 将 html 转换为文本

css - 如何将 <h1> 元素放在图像下方?