java - 通过哈希集搜索文件中的单词

标签 java loops java.util.scanner hashset

我有一个 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/

相关文章:

java - 带有自定义异常 Java 的密码检查器无输出

java - Scanner 类的 nextInt 方法是 Java 中的访问器还是修改器?

java - 通话结束后重新开始 Activity 的正确方法

java - 如何使用 ANT Build 排除特定的 java 文件夹和 java 文件

java - 使用 jdbc 进行 db2 查询时出错

loops - Excel VBA 中的循环

java - 扫描程序在使用next()或nextFoo()之后跳过nextLine()吗?

java - 如何知道自定义 Java 节点是否包含在第二个节点中?

c# - 使用 break 而不是 while(condition) 的 While 循环

php - Bootstrap - 模态仅显示第一项