- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用字典单词的哈希表编写拼写检查器。我目前正在尝试将文本文档中的单词与哈希表中的值进行比较,以查看其拼写是否正确,但无论我做什么, 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/
我正在尝试使用 HashMap 和数组为 6 支球队创建一个赛程列表。我有 6 个团队。每支球队必须互相比赛两次。我将从原始团队列表中随机删除每个团队并将其添加到新列表中。这个新列表被添加到 Hash
我有一个巨大的 float 组,我必须从中删除重复项。我尝试创建一个哈希表来填充唯一值并将其传递回另一个数组并返回它。问题出在 containsValue 方法中,该方法总是返回 false,因此所有
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 4 年前。 我正在尝试使用字典单词的哈希表编写拼写检查器。我目前正在尝试将文本文档
我有一个 HashSet,其中包含通过读取二进制文件生成的自定义对象。我还有一个通过读取 DBF 文件的每一行生成的字典。两者都有一个索引属性,它们相互对齐。例如,我的词典中的第 10 项将与我的 H
我正在开发一个程序来验证 map 是否包含某些值集。 Map API 有一种称为 map.containsValue(string) 的方法。但是,此方法将完整的字符串验证为值。 import jav
我有一个 HashMap,我需要通过它的整数值来获取一个项目。我注意到有一个 containsValue() 函数,但看起来我仍然必须遍历 map 才能找到正确的索引。 我的问题是;如果之后需要遍历它
我有一个字典,我在其中将一个键和 2 个值存储在一个元组中,如下所示: variableDictionary = new Dictionary>(); 所以我的字典是:key, 我想检查 value
我得到了一个O(n)时间复杂度的问题: “给定一个数字列表和数字 x。查找列表中是否有 2 个数字加起来为 x?” 这是我的解决方案: public class SumMatchResult {
因此,我试图在 powershell 中创建一个函数,该函数将用户所属的所有组嵌套在广告中。我将这些组映射到一个哈希表中,使用 AD 中的通用名称作为值。 这很顺利,该函数返回一个包含我所有组的哈希表
为什么会发生这种情况以及如何避免?我认为它只检查引用,但这不是很方便。 import java.util.LinkedHashMap; public class CheckingLinkedHashM
java中的containsValue是如何工作的?它是否将给定对象与 map 中已有的值进行比较?如果是这样,为什么这不起作用? 我有以下类:class Node{ 整数n; 整数重量; 字符串 v
如果我在类型和值中输入了错误的值,我希望我的程序显示错误消息。 错误消息适用于类型,但不适用于值。这是怎么回事? Map Heart = new HashMap(); Heart.put("A", "
我正在尝试将我用 Java 编写的算法转换为 Scala,但我在使用 Java 中的 containsValue() 方法时遇到了问题。我想做一些类似于 if (hashMap.containsVal
我有一本映射到摩尔斯电码的所有字母表 Dictionary data = new Dictionary(); data.Add("a", ".-"); data.Add
我有一张 map 如下: final Map> existingNames = this.getExistingCIs(_actual,_names.keySet()); 它返回: {myobj43c
containsValue() 在 AbstractMap 类中实现如下: public boolean containsValue(Object value) { Iterator> i =
我有一个下面的程序,其中有一个 HashMap 。 HashMap 的键是简单的整数,值是整数数组。程序如下: Map myMap = new HashMap(); myMap.put("EvenN
我正在实现一个 HashMap,即使字符重复,它也总是返回 false。我尝试了其他堆栈溢出问题中给出的解决方案,但没有帮助,带有函数 contains() 的哈希表也是如此。 HashMap ht
我想打印从扫描仪对象到达这里的选定键和值。我尝试过这种方式,发现有区别: TreeMap oldbooks = new TreeMap(); oldbooks.put("WaltDis
是否有有效的方法来检查字典中的值,一旦知道该值存在,然后找到该值的键? //检查字典已经有sortedWord(即key是word,value是sortedWord) if (anagramDict
我是一名优秀的程序员,十分优秀!