Word Checker

This is a short program which utilizes a TreeSet to respond to a user on whether a word exists in the English Lexicon or not. This program operates on a file “english.lex“.

The file provided is in a .lex format, but may be altered, and can be opened in any text editor.

Screen Shot 2018-01-28 at 11.38.20 AM

/**
 * Class which searches the English lexicon and lets user know if an entered word is
 * a word in the English language.
 *
 * @author AidanTakami
 * @since 1.0
 *
 */

import java.util.Scanner;
import java.util.TreeSet;
import java.io.File;
import java.util.ArrayList;

public class WordChecker {
	public static final String FILE_NAME = "english.lex";
	public static TreeSet<String> lexTree = new TreeSet<String>();

	/**
	 * Method which loads the file FILE_NAME into an ArrayList and then into lexTree
	 */
	public static void makeTree(){
		ArrayList<String> lexList = new ArrayList();
		//tries to scan the file and put all lines into an ArrayList.
		try{

			//Creates new file object, a new scanner, and scans the file into the Scanner
			File newFile = new File(FILE_NAME);
			Scanner fileScan = new Scanner(System.in);
			fileScan = new Scanner(newFile);

			//loops through Scanner, adding each nextLine to ArrayList
			while(fileScan.hasNextLine()){
				lexList.add(fileScan.nextLine());
			}
		}
		//Catches an exception which I saw online
		catch(Exception ex){
			ex.printStackTrace();
		}

		//iterates through arrayList, adding each String to lexTree
		for(int rep = 0; rep < lexList.size(); rep++){ 			lexTree.add(lexList.get(rep)); 		} 	} 	 	/** 	 * takes a string given by the user and returns whether it is contained in the  	 * lexTree or not. 	 *  	 * @param word  String given by the user. 	 * @return boolean 	 * @since 1.0 	 */ 	public static boolean isWord(String word){ 		return lexTree.contains(word); 	} 	 	/** 	 * Main function for testing 	 *  	 * @since 1.0 	 */ 	public static void main(String[] args){ 		//makes scanner, tree, and prints intro 		makeTree(); 		Scanner input = new Scanner(System.in); 		System.out.println("Welcome to the Word Checker!"); 		System.out.println("Loading file " + FILE_NAME + " which contains " + lexTree.size() + " words!"); 		System.out.println("Please enter a word or hit enter to quit."); 		 		//loop takes user input and returns answer 		while(true){ 			System.out.print("> ");

			//stores user input in String
			String word = input.nextLine();
			System.out.println();;

			//if string is empty, signifying enter key, breaks loop
			if(word.isEmpty()) break;

			//calls isWord, and responds accordingly
			boolean lexBool = isWord(word);

			if(lexBool){
				System.out.println(word + " is a valid word");
			}
			else{
				System.out.println(word + " is NOT a valid word.");
			}

		}
		System.out.println("Goodbye!");
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s