import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* LexiconApp.java ** This Java appication builds a lexicon containing the words that appear ** in a text file whose name is specifed by the first "run argument". ** The second run argument specifies the capacity of the lexicon, in ** terms of how many words it should be capable of storing/remembering. ** ** After the lexicon is built, the program reports how many words were ** placed in it, how many words appeared in the file but were not placed ** into it (due to its limited capacity), and how many total occurrences ** of words are in the file. (Of course, typically there will be many ** more occurrences of words than distinct words, because many words ** tend to occur multiple times.) ** ** After that, the program enters a "query phase" in which the user is ** invited (repeatedly) to enter a word. In response, the program ** reports whether or not that word is in the lexicon. Upon the user ** entering the empty string, the program terminates. ** ** Authors: R. McCloskey and P.M.J. ** Date: December 2022 */ public class LexiconApp { public static void main(String[] args) throws FileNotFoundException { System.out.println("Welcome to LexiconApp!"); // Use args[0] as the name of the file containing the text // to be read, and construct a Scanner for that purpose. Scanner input = new Scanner(new File(args[0])); // Use args[1] as the capacity (i.e., number of elements) of the // array to be used to store the words read from the iput file. final int N = Integer.parseInt(args[1]); String[] words = new String[N]; // where the words are stored int indexOfFirstEmptyElement = 0; // location to store next "new" word int wordOccurrences = 0; // to count # occurrences of words in file int wordsIgnored = 0; // to count how many distinct words were not // placed into words[] due to it being full // For each occurrence of a word in the input file, check to see if // that word is already in words[]. If not, and if words[] is not // filled to its capacity, place the word into the first "empty" // element of words[]. while (input.hasNext()) { String word = input.next(); // Read the next word wordOccurrences = wordOccurrences + 1; // Search for the word in words[0..indexOfFirstEmptyElement) int k = 0; while (k != indexOfFirstEmptyElement && !words[k].equals(word)) { k = k+1; } if (k == indexOfFirstEmptyElement) { // word does not occur in words[], so insert it if there is room. if (indexOfFirstEmptyElement != N) { words[indexOfFirstEmptyElement] = word; indexOfFirstEmptyElement = indexOfFirstEmptyElement + 1; } else { wordsIgnored = wordsIgnored + 1; } } } // Report the number of word occurrences, the number of words placed // into the lexicon, and the number of words that occurred at least // once but were not placed into the lexicon (due to it being full). System.out.printf("There are %d occurrences of words in the file.\n", wordOccurrences); System.out.printf("%d words were placed into the lexicon.\n", indexOfFirstEmptyElement); System.out.printf("%d other words occurred in the file but " + "were not placed into the lexicon.\n", wordsIgnored); Scanner keyboard = new Scanner(System.in); // Pause to allow user to read output just produced. System.out.print("\nPress ENTER to continue >:"); keyboard.nextLine(); System.out.println(); // For each occupied element of words[], display its index and // the word stored there. for (int i = 0; i != indexOfFirstEmptyElement; i = i+1) { System.out.printf("%3d: \"%s\"\n", i, words[i]); } System.out.println(); // Now enter the query phase of the program, during which (repeatedly) // the user enters a word and the program responds by indicating the // index of the element of words[] where it is stored or, in case there // is no such element, displays a message indicating that fact. System.out.println("Query Phase: (empty string to quit)"); String word; do { System.out.print("\nEnter word:>"); word = keyboard.nextLine().trim(); if (word.length() != 0) { // Search for word in words[0..indexOfFirstEmptyElement) int k = 0; while (k != indexOfFirstEmptyElement && !words[k].equals(word)) { k = k+1; } // Check to see if word was found and report accordingly if (k != indexOfFirstEmptyElement) { //System.out.printf("\"%s\" found at %d\n", word, k); System.out.println("YES"); } else { //System.out.printf("\"%s\" not found!\n", word); System.out.println("NO"); } } } while (word.length() != 0); System.out.println("Goodbye."); } }