How to find a word in multiple text files ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maral
    New Member
    • Aug 2010
    • 3

    How to find a word in multiple text files ?

    Hi every one, this is my first post here!

    I'm using GATE toolkit for information retrieval and text analysis, but i really need java for some parts.

    I have managed to find a specific word in one text file, but i need to look for a specific word, in multiple text files. all of the files are ".txt" and they are in the same folder but each of them with different names. I have written the following code: but i receive an error that i don't know how to solve:


    Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Find {
    
        public static void main(String s) throws IOException {
            int count =0;
            String srcDir = s;
            File folder = new File(srcDir);
            File[] listOfFiles = folder.listFiles();
            if (listOfFiles.length > 0) {
                for (int i = 0; i < listOfFiles.length; i++) {
                    if (listOfFiles[i].isFile()) {
                        Scanner a = null;
                        a = new Scanner(new BufferedReader(new FileReader(srcDir + listOfFiles[i].getName())));
                    while (a.hasNext()){
                    String words = a.next();
                    if (words.equals("good")){
                       count++;
                    }
    
                }
        System.out.println("the total is:" + count);
    
            }
        }
        }
        }
        }
    and this is the error that i get:

    java.lang.NoSuc hMethodError: main
    Exception in thread "main"

    I would appreciate any helps and comments.
    Maral
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Change your main function to

    Code:
    public static void main(String[] s)
    Regards
    Dheeraj Joshi

    Comment

    • maral
      New Member
      • Aug 2010
      • 3

      #3
      Dheeraj, thanks for your comment.
      with few more changes, it helped to solve my problem.

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Yes. You should also change following lines.

        Code:
        String srcDir = s;
        Regards
        Dheeraj Joshi

        Comment

        • software tools
          New Member
          • Jul 2014
          • 9

          #5
          Also avoid hard coding like, "if word equals good",etc. try to write a generic program.

          Comment

          • adhawan
            New Member
            • Jul 2021
            • 1

            #6
            Hi,could you please tell me what more changes you made,as i am also looking forward for same scenario.

            Comment

            Working...