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

Java: Write a program Find that searches all files specified on the command line

ID: 664402 • Letter: J

Question

Java: Write a program Find that searches all files specified on the command line and prints out all lines containing a reserved word. Start a new thread for each file. For example, if you call

then the program might print


I'm not getting any output from my program:

import java.io.FileInputStream;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InterruptedIOException;
//P20.9 Find a target string in a file and print the line
public class FindRunnable implements Runnable
{
   private String target;
   private String fileName;

   public FindRunnable(String aTarget, String aFileName)
   {
target = aTarget;
fileName = aFileName;
   } // end of constructor

   public void run()
   {
try
{
   Scanner in = new Scanner(new FileInputStream(fileName));
   while (in.hasNextLine())
   {
String line = in.nextLine();
  
if ((line.indexOf(target)) != -1)
{
   System.out.println(fileName + ": " + line);
}
  
   } // end of while
} // end of try

catch (FileNotFoundException e)
{

   System.out.println(fileName + " not found ");
}

catch (IOException e)
{
   System.out.println(e);
}
   } // end of run method
} // end of FindRunnable

CLASS: FIND

public class Find
{
   public static void main(String[] argv)
   {
for (int i = 1; i < argv.length; i++)
{
   FindRunnable finder = new FindRunnable(argv[1], argv[i]);
   Thread t = new Thread(finder);
   t.start();
} // end of for loop
   } // end of main
} // end of find classimport java.io.*;

  java Find Buff report.txt address.txt Homework.java  

Explanation / Answer

I tried to run this program and got complilation error

1) class FindRunnable is public, should be declared in a file named FindRunnable.java public class FindRunnable implements Runnable .

2) class Find is public, should be declared in a file named Find.java public class Find.

3) Please remove unnecessary imports.Only below imports you can use.
import java.io.*;
import java.util.*;

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