Jump to content

Java file search

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Anemone

Anemone

    Newbie

  • Members
  • PipPip
  • 15 posts
hi Im having some problems figure out why my code doesent work

Write a program Search that searches all files specified on the command line and prints out all lines containing a keyword. For example, if you call

java Search Buff code.txt address.txt Assignment.java

then the program might print

code.txt: Buffer style lunch will be available at the

here is my code

class Search {


    public static void main(String args[]){


        BufferedReader br = null;

        String pattern, s;

        int numInputFiles, ctr;


        numInputFiles = args.length - 1;

        pattern = new String ();

        s = new String ();


        pattern = args[0];    

        for (ctr = 0; ctr < numInputFiles; ctr++){

            try {

                br = new BufferedReader (new FileReader (args[ctr+1]));

                s = br.readLine ();

            }catch (Exception e) {

                System.out.println (e);

            }

            

            while (s!=null) {

                if( s.indexOf (pattern) >= 0) {

                    System.out.print(args[ctr+1] + ": ");

                    System.out.println (s);

                }    

            

                try {

                    s = br.readLine ();

                } catch (Exception e) {

                    System.out.println (e);

                }

            }

        }

        try{

            br.close();

        } catch(Exception e){

            System.out.println(e);

        }    


    }

}



Im getting some problems here and I hope you
can put me in the right direction

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
What exactly are the problems you're getting?
I think it all looks correct.. i would only do br.close() at the end of each for-loop. But i doubt if that causes serious problems :c-blink:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users