Anthony J
Anthony J

Reputation: 581

Searching through a Hashset for words within a file

I've got a Hashset with my dictionary of words in it.

What I'm trying to do is to individually scan words from the file checkMe to see whether they exist in my HashSet.

When a word doesn't exist, I need to trigger a number of actions (which I won't get into).

For now, I'd like some advice as to how I take words from my scanned file and check them against my HashSet.

Something like:

if (dicSet does not contain a word in checkMe) {
    da da da 
}

Also, I want to be able to loop through checkMe to make sure each word is checked through dicSet until I reach an error.

My code so far:

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);
  }
}

Upvotes: 1

Views: 823

Answers (1)

Jiri Tousek
Jiri Tousek

Reputation: 12440

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

That's the basic idea at least. For real use, you'd have to add a ton of error-checking and exceptional states handling (what if your input contains numbers? What about ., - etc?)

Upvotes: 1

Related Questions