Hey guys,

First time poster here, although I've been trolling for a little while now. I'm in a Java 2 class and having trouble completing a practice program. It's supposed to search files that are given by the user at a command prompt for a specific word also given. Here is what I am working with for instructions

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 Buff report.txt address.txt Homework.java

then the program might print the foll. (i.e., all the lines that contain the word "Buff"):

report.txt: Buffet style lunch will be available at the
address.txt: Buffet, Warren|11801 Trenton Court|Dallas|TX
address.txt: Walters, Winnie|59 Timothy Circle|Buffalo|MI
Homework.java: BufferedReader in;

Here is what I have so far.

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import java.io.*;

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


public class HW4c
{
    public static void main(String[] args)
    {
        //String fileName1 = args[0];
        String toFind;  

        try
        {

            Scanner input = new Scanner(System.in);
            toFind = input.next();

            String fileName1  = args[args.length - 1];


            for( int i = 0; i < args.length; i++ ) //make ==
            {


                BufferedReader in = new BufferedReader( new FileReader( fileName1 ) );

                String line = in.readLine();

                while(line != null)
                {
                    line = in.readLine();   
                    if(line.indexOf(toFind, 1) == -1)
                    {
                        System.out.println(line);
                    } 

                }

            }

        }
        catch (IOException e) 
        {
            e.printStackTrace();    
        }       

    }

}

Any help would be amazing!

First thing, your class should be called "Find" based on your command-line requirements.

The only imports necessary are:
import java.io.*;
import java.util.*;

Since all of the parameters are entered on the command-line, Scanner is not necessary.

The first parameter, then, is the search element, so:
String toFind = args[0];

...but maybe after you check to make sure there are parameters.

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.