[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

implement threads with string pattern search

Asked by kulina in Java Programming Language, New to Java Programming

Tags: java, advace, line

I'm writting a grep-alike Java application which will take a string pattern as a first command line argument, and a list of files as the rest of arguments. Application will search every file.  When the string is found, the application will print the file name, the line number and the entire content of the line where the string was found, like this:

filename - linenumber: content of line where the searched string was found

The code below works fine, but I need to implement threads, and synchronize them. Every file search need to be done in a separate thread and the result needs to be printed in the order of the given files on the command line.

Invocation should be like this: java  Grep abc File1 File2 File3

And the output could look like this:
File1 - 28: xxxabcxxxxxxxxxxxx
File2 - 2:  xxxxxxxxxxxxxxxxxxabcxxxxxx
File2 -234: xxxxxxxxxabcx
File3 - 24: abcxxxxxxxxxxxxxxxxxx

My problem is that the files are not diplayed in the order they are given.
Every time I run it I get different order.
Can somebody explain me how to synchronize these threads?
A sample code would be great.

Thank you in advace.

////// here is my code  //////
import java.io.*;
import java.util.*;

public class Grep1 extends Thread {

      public String pattern;
      public String fileName;

      public Grep1( String threadName ){
            super( threadName );
      }

      public void run(){
            FileInputStream in;
        try {
                  in = new FileInputStream( fileName );
                  grep( pattern, in, fileName );
            in.close();
        }
            catch ( IOException e ) {
                  System.out.println( "Error: file " + fileName + " not found" );
        }
      }

    public synchronized void grep( String pattern, FileInputStream in,
                                String file )
                                    throws IOException{

            boolean found = false;
            int lineNumber = 0;
        PrintStream out = System.out;
        BufferedReader data = new BufferedReader( new InputStreamReader(in) );
            LineNumberReader infile = new LineNumberReader( data );
        String line = infile.readLine();

        while ( line != null ) {
            if ( line.indexOf(pattern) >= 0 ){
                        lineNumber = infile.getLineNumber();
                out.println( file + " - " + lineNumber + ": " + line );
                        found = true;
                  }
            line = infile.readLine();
        }
            if( found == false )
                  out.println( "Couldn't find \"" + pattern + "\" in " + file );
    }

    public static void main( String[] args ){

        int numOfArgs = args.length;

        if ( numOfArgs < 2 ) {
            System.err.println( "Usage: java Grep [pattern] file1 file2 ...");
                  return;
        }
            else{
                  Grep1[] g = new Grep1[ numOfArgs-1 ];
                  for ( int i=0; i<g.length; i++ ){
                        g[i] = new Grep1( args[i+1] );
                        g[i].pattern = args[0];
                        g[i].fileName = args[i+1];
                        g[i].start();
                  }
            }
    }
}
/////// the end /////////
 
Loading Advertisement...
 
[+][-]07/03/00 10:27 PM, ID: 3171577Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/03/00 10:28 PM, ID: 3171590Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/03/00 11:14 PM, ID: 3171735Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/03/00 11:39 PM, ID: 3171894Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/04/00 12:08 AM, ID: 3172033Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/04/00 12:38 AM, ID: 3172210Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/04/00 01:18 AM, ID: 3172470Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/04/00 01:56 AM, ID: 3172717Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/04/00 03:45 AM, ID: 3173734Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/04/00 05:06 AM, ID: 3174357Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/19/00 09:07 PM, ID: 3474630Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/19/00 09:09 PM, ID: 3474638Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/20/00 12:10 AM, ID: 3475915Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Java Programming Language, New to Java Programming
Tags: java, advace, line
Sign Up Now!
Solution Provided By: diakov
Participating Experts: 5
Solution Grade: A
 
[+][-]07/24/00 01:49 PM, ID: 3552196Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/24/00 02:02 PM, ID: 3552492Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/24/00 02:02 PM, ID: 3552493Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/24/00 04:04 PM, ID: 3554795Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81