Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

findFirstMatch

Language/Type: Java arrays file processing Scanner
Author: Marty Stepp (added by Roy McElmurry)

Write a method named findFirstMatch that accepts as its parameters a Scanner for an input file and an array of Strings keywords representing a list of keywords in a search. Your method will read lines from its input Scanner and should return the line number of the first line in the file that contains one or more words from keywords. If none of the keywords are found in the file, your method should return a -1. The search should be case-insensitive, so if a keyword was "banana", the line "yummy baNAna split" would be considered a line that contains the keyword. Your method should also match whole words only, so if the only keyword in the array was "ball", the line "football game" would not be considered a match.

For example, consider the following input file saved in sidewalk.txt, consisting of 6 lines:

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.
Scanner input = new Scanner(new File("sidewalk.txt"));
Arrays Call / Returned Value
String[] k1 = {"place", "winds"}; findFirstMatch(input, k1) returns 1
String[] k2 = {"dinosaur", "PITS", "pots"}; findFirstMatch(input, k2) returns 3
String[] k3 = {"chalk", "row", "g", "ends"}; findFirstMatch(input, k3) returns -1
String[] k4 = {"to"}; findFirstMatch(input, k4) returns 6

You may assume that none of the words in the keywords array contain spaces, i.e. all keywords are single whole words, and the array contains at least one element. Do not modify the elements of the keywords array.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.