NeedlesHaystackTest is a Java console application that finds words in a local plaintext file. You can enter up to 5 words to find in the plaintext file you specify.

The words you search for are called needles, and the plaintext file comprises the haystack in which to search.

Download the compiled NeedlesHaystackTest.class file or download the java source and compile on your own system. The code was written and compiled using Java SE (Oracle JDK) 11.

Before running the application:

Running the Application

To run the application:

java com.alamedatechlab.googtest.NeedlesHaystackTest

The application prompts you to enter the number of words that you want to find in the haystack, the name fo the file to search, and the words themselves. Respond to the prompts with your choices:

  1. Enter 1, 2, 3, 4, or 5
  2. Enter each word that you want to find
  3. Enter the filename that comprises the text of your haystack. Specify the full path to the file in the context of your system and its classpath.

Entries are validated before proceeding, and an array of the specified size is created.

Processing Summary

The haystack is split into an array of individual words using a regular expression identifying quotation marks, apostrophe, tab, newline, form feed, and return characters and creating word tokens from the isolated substrings.

The length of the haystack array is then used to drive the comparison of each specified needle to each word in the haystack array.

A counter keeps track of the number of instances of each word found in the haystack array.

After all specified words are processed by the loop, each word and its total is printed at the console.