Okay so I'm making a basic spellchecker. I'm using a relatively small dictionary with about 1000 of the most common english words, all on separate lines in a text file. I'm on windows so I can't use the Linux dictionary.

Anyways, It's not working. And I'm not quite sure why. It may just be because I can't quite tell which classes to use as far as which reader and which tokenizer. Here's my code, hope you can help.

Please note that I'm required to use the Set class for my dictionary. So please do not post a solution wondering why I'm using Set.

Thanks!

EDIT: after testing a few things I've realized that either the dictionary Set probably not being created correctly. I'm not sure why? anyone got an explaination?

import java.io.*;
import java.awt.*;
import java.util.*;


public class spellcheck
{




    public static String spellC(String checkedFile)
    {
        Set dict = new HashSet();
        String returnVal = "";
        //creating a dictionary set
        try{
        Reader fileReader = new BufferedReader(new FileReader("C:/Users/KB/Documents/NetBeansProjects/Barta_Kyle_Ass2/test/dictionary.txt"));
        StringTokenizer st = new StringTokenizer(fileReader.toString());
        while(st.hasMoreTokens())
        {
            dict.add(st.nextToken());

        }
        }
        catch(IOException e){
            returnVal = "IOException at dictionary making process";
        }

        try{
        Reader checkedRead = new BufferedReader(new FileReader(checkedFile));
        StringTokenizer checkedToken = new StringTokenizer(checkedRead.toString());
        while(checkedToken.hasMoreTokens())
        {
            
                if(dict.contains(checkedToken.nextToken()))
                {
                    returnVal = "";
                }
                else
                {
                    returnVal = checkedToken.nextToken();
                }
            
        }
        }
        catch(IOException i){
            returnVal += "\n IOException at Checking File";
        }
        return returnVal;
    }


}

Update. I tried fixing it but this won't work either as it takes an extremely long time. Not sure why.

import java.io.*;
import java.awt.*;
import java.util.*;


public class spellcheck
{




    public static String spellC(String checkedFile)
    {
        Set dict = new HashSet();
        String returnVal = "";
        //creating a dictionary set
        try{
        BufferedReader fileReader = new BufferedReader(new FileReader("C:/Users/KB/Documents/NetBeansProjects/Barta_Kyle_Ass2/test/dictionary.txt"));
        String line = fileReader.readLine();
        while(line!=null){
           StringTokenizer st = new StringTokenizer(line," \n");
           while(st.hasMoreTokens())
            {
                dict.add(st.nextToken());
            }
        }
        }
        catch(IOException e){
            returnVal += "IOException at dictionary making process";
        }

        try{
        BufferedReader checkedRead = new BufferedReader(new FileReader(checkedFile));
        String line2 = checkedRead.readLine();
        while(line2!=null)
        {
        StringTokenizer checkedToken = new StringTokenizer(line2);
        while(checkedToken.hasMoreTokens())
        {
            
                if(dict.contains(checkedToken.nextToken()))
                {
                    returnVal += "";
                }
                else
                {
                    returnVal += checkedToken.nextToken();
                }
            
        }
        }
        }

        catch(IOException i){
            returnVal += "\n IOException at Checking File";
        }
        catch(NoSuchElementException x){
            returnVal += "\n End of file";
        }
        return returnVal;
    }


}

Fixed it. Nevermind. Here's my final code that fixed it if anyone needs it:

import java.io.*;
import java.awt.*;
import java.util.*;


public class spellcheck
{




    public static String spellC(String checkedFile)
    {
        Set dict = new HashSet();
        String returnVal = "";
        //creating a dictionary set
        try{
        BufferedReader fileReader = new BufferedReader(new FileReader("C:/Users/KB/Documents/NetBeansProjects/Barta_Kyle_Ass2/test/dictionary.txt"));
        String line = fileReader.readLine();
        while(line!=null){
           StringTokenizer st = new StringTokenizer(line,"\n");
           while(st.hasMoreTokens())
            {
                dict.add(st.nextToken());
            }
           line = fileReader.readLine();
        }

        fileReader.close();
        }
        catch(IOException e){
            returnVal += "IOException at dictionary making process";
        }


        try{
        BufferedReader checkedRead = new BufferedReader(new FileReader(checkedFile));
        String line2 = checkedRead.readLine();
        while(line2!=null)
        {
        StringTokenizer checkedToken = new StringTokenizer(line2," ");
        while(checkedToken.hasMoreTokens())
        {
            String currTok = checkedToken.nextToken();
                if(dict.contains(currTok))
                {
                    returnVal += "";
                }
                else
                {
                    returnVal += currTok +"\n";
                }

            
        }
        line2 = checkedRead.readLine();
        }
        checkedRead.close();
        }

        catch(IOException i){
            returnVal += "\n IOException at Checking File";
        }
        catch(NoSuchElementException x){
            returnVal += "\n End of file";
        }
        return returnVal;
    }


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.