我有一个 Hashset
,其中包含我的单词词典。
我想做的是逐个扫描文件 checkMe
中的单词,看看它们是否存在于我的 HashSet
中。
当一个单词不存在时,我需要触发许多操作(我不会涉及)。
现在,我想要一些关于如何从扫描文件中提取单词并根据我的 HashSet
检查它们的建议。
类似于:
if (dicSet does not contain a word in checkMe) {
da da da
}
此外,我希望能够循环检查 checkMe
以确保通过 dicSet
检查每个单词,直到出现错误。
到目前为止我的代码:
import java.util.*;
import java.io.*;
public class spelling{
public static void main(String args[]) throws FileNotFoundException {
//read the dictionary file
Scanner dicIN = new Scanner(new File("dictionary.txt"));
//read the spell check file
Scanner spellCheckFile = new Scanner(new File("checkMe.txt"));
//create Hashset
Set <String> dicSet = new HashSet<String>();
//Scan from spell check file
Scanner checkMe = new Scanner(spellCheckFile);
//Loop through dictionary and store them into set. set all chars to lower case just in case because java is case sensitive
while(dicIN.hasNext())
{
String dicWord = dicIN.next();
dicSet.add(dicWord.toLowerCase());
}
//make comparisons for words in spell check file with dictionary
if(dicSet){
}
// System.out.println(dicSet);
}
}
最佳答案
while(checkMe.hasNext())
{
String checkWord = checkMe.next();
if (!dicSet.contains(checkWord.toLowerCase())) {
// found a word that is not in the dictionary
}
}
这至少是基本的想法。对于实际使用,您必须添加大量错误检查和异常状态处理(如果您的输入包含数字怎么办?.
、-
等怎么样?)
关于java - 通过哈希集搜索文件中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34091747/