By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

How to find a word in multiple text files ?

P: 3
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:


Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class Find {
  5.  
  6.     public static void main(String s) throws IOException {
  7.         int count =0;
  8.         String srcDir = s;
  9.         File folder = new File(srcDir);
  10.         File[] listOfFiles = folder.listFiles();
  11.         if (listOfFiles.length > 0) {
  12.             for (int i = 0; i < listOfFiles.length; i++) {
  13.                 if (listOfFiles[i].isFile()) {
  14.                     Scanner a = null;
  15.                     a = new Scanner(new BufferedReader(new FileReader(srcDir + listOfFiles[i].getName())));
  16.                 while (a.hasNext()){
  17.                 String words = a.next();
  18.                 if (words.equals("good")){
  19.                    count++;
  20.                 }
  21.  
  22.             }
  23.     System.out.println("the total is:" + count);
  24.  
  25.         }
  26.     }
  27.     }
  28.     }
  29.     }
  30.  
and this is the error that i get:

java.lang.NoSuchMethodError: main
Exception in thread "main"

I would appreciate any helps and comments.
Maral
Aug 24 '10 #1
Share this Question
Share on Google+
4 Replies


Dheeraj Joshi
Expert 100+
P: 1,123
Change your main function to

Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] s) 
  2.  
Regards
Dheeraj Joshi
Aug 24 '10 #2

P: 3
Dheeraj, thanks for your comment.
with few more changes, it helped to solve my problem.
Aug 24 '10 #3

Dheeraj Joshi
Expert 100+
P: 1,123
Yes. You should also change following lines.

Expand|Select|Wrap|Line Numbers
  1. String srcDir = s;
  2.  
Regards
Dheeraj Joshi
Aug 25 '10 #4

P: 9
Also avoid hard coding like, "if word equals good",etc. try to write a generic program.
Jul 7 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.