findFirstMatch

Category: Line-Based File Processing
Author: Victoria Kirst
Book Chapter: 6.3
Problem: findFirstMatch
Write a static method named findFirstMatch that accepts as its parameters a Scanner for an input file and an array of Strings representing a list of keywords in a search.

Your findFirstMatch method will read lines from its input Scanner and it should return the line number of the first line in the file that contains one or more words from the keywords array. (The line numbers are 1-based, so the first line of the file is line 1.) If none of the keywords are found in the file, your method should return a -1. The search should be case-insensitive, so if "banana" is a keyword, the line "baNAna split" would be considered a line that contains the keyword. Your method should also match whole words only, so if the word "ball" is a keyword, the line "soccer or football" should not be considered a match for that keyword.

For example, consider the following input file saved in sidewalk.txt:

     Let us leave this place where the smoke blows black
     And the dark street winds and bends.
     Past the pits where the asphalt flowers grow
     We shall walk with a walk that is measured and slow,
     And watch where the chalk-white arrows go
     To the place where the sidewalk ends.

The following table shows some calls to your method and their expected results

+---------------------------------------------------------+------------------+
|  Array                                                  |  Returned Value  |
+---------------------------------------------------------+------------------+
| Scanner input = new Scanner(new File("sidewalk.txt"));  |                  |
| String[] k1 = {"place", "winds"};                       |        1         |
| findFirstMatch(input, k1);                              |                  |
+---------------------------------------------------------+------------------+
| Scanner input = new Scanner(new File("sidewalk.txt"));  |                  |
| String[] k2 = {"dinosaur", "PITS", "pots"};             |        3         |
| findFirstMatch(input, k2);                              |                  |
+---------------------------------------------------------+------------------+
| Scanner input = new Scanner(new File("sidewalk.txt"));  |                  |
| String[] k3 = {"chalk", "row", "g", "ends"};            |        -1        |
| findFirstMatch(input, k3);                              |                  |
+---------------------------------------------------------+------------------+
| Scanner input = new Scanner(new File("sidewalk.txt"));  |                  |
| String[] k4 = {"to"};                                   |        6         |
| findFirstMatch(input, k4);                              |                  |
+---------------------------------------------------------+------------------+

Assume that none of the words in the keywords array contains spaces, i.e. all keywords are non-null, non-empty single whole words.