Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program Find that searches all files specified on the command line and p

ID: 3653633 • Letter: W

Question

Write a program Find that searches all files specified on the command line and prints out all lines containing a reserved word. 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 reserved word is always the first command line argument. Here is a sample program run: java Find lamb mary1.txt mary2.txt mary1.txt: Mary had a little lamb, mary1.txt: little lamb, little lamb, mary1.txt: Mary had a little lamb, its fleece was white as snow. mary1.txt: and everywhere that Mary went, the lamb was sure to go. mary2.txt: it made the children laugh and play to see a lamb at school. Use the following class as your main class: import java.io.FileNotFoundException; import java.io.File; 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 FileNotFoundException { if (args.length < 2) { System.out.println("Usage: Find keyword sourcefile1 sourcefile2 . . ."); return; } String keyword = args[...]; for (int i = 1; i < ...; i++) { String filename = args[...]; . . . } } }

Explanation / Answer

public class SearchWord { static String keyword; static int lineID; public static void main(String[] args) throws Exception Scanner fileScanner = new Scanner(new File(fileName)); if(args.length < 2) { System.out.print("Usage: java Search keyword sourcefile1 sourcefile2 sourcefile3"); return; } keyword = args[0] for(i = 1; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote