java - Java Hashtable containsValue() 不起作用
问题描述
我正在尝试使用字典单词的哈希表编写拼写检查器。我目前正在尝试将文本文档中的单词与哈希表中的值进行比较,以查看它是否拼写正确,但无论我做什么,即使我知道不是, 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
解决方案
在您的方法中更改p.x==this.x
为。p.x.equals(this.x)
equals()
有关更多信息,请了解如何比较 Java 中的字符串。
推荐阅读
- button - Blazor:调用 StateHasChanged 时触发的按钮 OnClick 事件
- ios - 处理相机快门声音时如何继续播放音频?
- ruby-on-rails - 自定义控制器方法在 Rails 中触发 Action Mailer 时出现 500 错误(在 Heroku 上)
- python - 来自 nltk.corpus 的 Python wordnet。如何使用 wordnet 获得几个句子中每个单词的定义?
- javascript - 通过 javascript 和 android Chrome 实现 SpeechSynthesisUtterance() TTS
- javascript - 如何在 AntDesign 的 Table 组件中为溢出的文本添加省略号?
- azure-devops - 无法将 Azure Active Directory 连接到 Azure DevOps,因为 1 个用户具有多个具有相同 UPN 的活动身份
- git - 项目中的 Atlassian Bitbucket 访问存储库
- python - 基于组合 Id 和 Date (Pandas) 的多个聚合
- visual-studio - 如何在 Visual Studio 中的 UWP 和 Win32 项目之间共享 C++ 代码?