I have an assignment that calls for me to Write a program Find that searches all files specified on the command line and prints out all lines containing a keyword. For example, if you call
java Find ring report.txt address.txt Homework.java
then the program might print
report.txt: has broken up an international ring of DVD bootleggers that
address.txt: Kris Kringle, North Pole
address.txt: Homer Simpson, Springfield
Homework.java: String filename;
The keyword is always the first command line argument
The main class provided by my instructor:
import java.io.IOException;
import java.io.FileReader;
import java. util.Scanner;
/**
This program searches files and prints out all lines containing a keyword.
*/
public class Find
{
public static void main(String[] args) throws IOException
{
if (args.length < 2)
{
System.out.println(
"Usage: Find keyword sourcefile1 sourcefile2 . . .");
return;
}
String keyword = args[0];
for (int i = 1; i < args.length; i++)
{
String filename = args[i];
. . . // add code here
}
}
}
What I'm trying to figure out are what methods from the string class are to be used for the search. Also do I need to use another for loop to print out the lines for the found keyword? Any help would be appreciated. thanks