在Hashset中搜索文件中的单词

时间:2015-12-04 15:28:29

标签: 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);
  }
}

1 个答案:

答案 0 :(得分:1)

while(checkMe.hasNext())
{
    String checkWord = checkMe.next();
    if (!dicSet.contains(checkWord.toLowerCase())) {
        // found a word that is not in the dictionary
    }
}

至少这是基本的想法。对于实际使用,您必须添加大量错误检查和异常状态处理(如果您的输入包含数字,该怎么办?.-等等?

相关问题