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

if ( line.indexOf(theWord) != -1 )
  System.out.println(line);

with line being the line of the input file and theWord being the word you're looking for.

PS: never allow your main method to throw an Exception, that way you'll never be able to avoid your application to crash when there's a problem.

Ok here is my code

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];
         FileReader reader = new FileReader(filename);
         Scanner in = new Scanner(reader);
         
         String line = in.nextLine();
         while(line != null)
         {
         
         if(line.indexOf(keyword) != -1)
         {
         System.out.println(line);
         } 
         }
      }
   }
}

That looks OK, but as stultuske pointed out, it's horrible practice to ignore your IOExceptions - much better to catch them and print an understandable error message yourself.

You could also use line.contains(keyword) rather than line.indexOf(keyword) != -1 because it's simpler and clearer

depends on what the OP means by 'finding the String'. if he just has to detect whether it is in there, contains is easier indeed, but if he needs to know the location or count the number of occurrences, indexOf is better

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.