Welcome to Chris Johnsons' Portfolio

G'day. My name is Christopher Johnson and you've reached my humble portfolio which contains bits and pieces that I've created over the years. I've decided to post as much as I can in order to show where I've come from and in what ways I've improved. Thanks for stopping by. I hope you enjoy what you see. If you have any questions then feel free to contact me. Chris.

Twitter Update

Follow me on Twitter

Navigation

My Websites

Most Recent Additions

Browse by Month

Content Syndication

Meta

Spelling Bot Code Snippets

BotSpellChecker.java – BotSpellChecker Class

public class BotSpellChecker {
	private static final String dictFile = "aspell.dic";
	private static final String customDictFile = "custom.dic";
	private static BotSpellChecker instance = null;
 
	private SpellChecker spellCheck;
 
	/**
	  * Gets the current instance of the BotSpellChecker.
	  * Singleton class used to prevent mulitple instances.
	  * 
	  * @return      the current instance
	*/
	public static BotSpellChecker getInstance() {
		if(instance == null) {
			instance = new BotSpellChecker();
		}
		return instance;
	}
 
	/**
	  * Creates Hash Map of the user dictionaries.
	*/
	private BotSpellChecker() {
		SpellDictionary dictionary = null;
		SpellDictionary customDictionary = null;
 
		try {
			dictionary = new SpellDictionaryHashMap(new File(dictFile), null);
		} catch (Exception e) {
			System.err.println("Could not correctly create the Dictionary Hash Map");
			System.exit(-1);
		}
 
		try {
			customDictionary = new SpellDictionaryHashMap(new File(customDictFile),
                                                        null);
		} catch (Exception e) {
			System.err.println("Could not correctly create the custom " + 
                                                     "Dictionary Hash Map");
			System.exit(-1);
		}
 
		spellCheck = new SpellChecker(dictionary);
		spellCheck.addDictionary(customDictionary);
	}
 
	/**
	  * Adds word to current dictionary hash map.
	  *
	  * @param  word word to be added to hash map
	*/
	public void addToDictionary(String word){
		spellCheck.addToDictionary(word);
	}
 
	/**
	  * Adds word to current dictionary hash map
	  * and writes it to the custom dictionary file.
	  * Assumes that one word is passed
	  *
	  * @param  word word added
	*/
	public void writeToDictionary(String word){
		try {
			FileWriter fw = new FileWriter(customDictFile, true);
			fw.write(word + "\n");
			fw.flush();
			fw.close();
		} catch(IOException e) {
			System.err.println("IOException Raised: Word may not be added" + 
                                                     " to dictionary");
			return;
		}
		addToDictionary(word);
	}
	/*
	More Code Here - Download the entire source code for rest
	*/
}