473,416 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to find a word in multiple text files ?

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
5 14599
Dheeraj Joshi
1,123 Expert 1GB
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
maral
3
Dheeraj, thanks for your comment.
with few more changes, it helped to solve my problem.
Aug 24 '10 #3
Dheeraj Joshi
1,123 Expert 1GB
Yes. You should also change following lines.

Expand|Select|Wrap|Line Numbers
  1. String srcDir = s;
  2.  
Regards
Dheeraj Joshi
Aug 25 '10 #4
Also avoid hard coding like, "if word equals good",etc. try to write a generic program.
Jul 7 '14 #5
adhawan
1 Bit
Hi,could you please tell me what more changes you made,as i am also looking forward for same scenario.
Jul 29 '21 #6

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

Similar topics

5
by: gwarning! | last post by:
Goal: Have multiple text files, each of various lengths, be concatenated together into one final consolidated text file. Problem: Since the names of the files to be concatenated can change from...
1
by: Charlie | last post by:
Hello, I have data in an Access table that I would like to export to multiple HTML tables. I would like to split the data in the Access table (about 92,000 records) into multiple HTML...
2
by: Chris Murphy via DotNetMonster.com | last post by:
Hey all, just wondering if anyone can point me in the right direction. I'm developing a solution that allows a user to store multiple text-based content (like code snippets, notes, documents etc.)...
5
by: Buddhist[CHinA] | last post by:
The text files are not only the .txt files, but also all ascii files. Thx.
9
by: malla | last post by:
Hi, I am trying to open a bunch of files and get data from them into one single file. I am sure this process can be automated if I name the files that I want to open in a regular pattern (say...
2
by: ManningFan | last post by:
I'm running through a database in a loop, and each time the loop finishes I would like to export the values of some variables to new text files. In reality it's more complex than this, but this is...
3
emaghero
by: emaghero | last post by:
Hello all, I want to open multiple txt files with similar names in C++ I have attempted this with the following code //Create as many txt files as there are valid propagation constants...
6
by: borthouth | last post by:
Hi, I have just started using Python and I am slowly getting into it. I wanted to make a little script to merge all files in a directory into one. All of these files will be text files. I...
3
by: ezra | last post by:
I need to run a monthly audit on a customer invoice database that prints off order totals per customer. Ex. If customer 1234 has 4 orders for the month I want to see the text file as follows: 1234...
2
by: as001 | last post by:
Hi, I'm writing a windows application in C# using VS 2003. I got stuck where it has to write multiple output text files. Here's my piece of code: for loop { Random r = new Random();...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.