-
Help with counting words in a file please
I have written a program to read in a directory of directories, process the files within the inner directory and to return a float value for each file.
The problem is that i need to add, for each individual directory the float values corresponding to the files within
So if a directory contains file1 =2 file2 =6 and file 3 = 9 for example i want to return 17.
static int indentLevel = -1;
static void ReadDir(File path) {
float sum[];
float max = 0;
int common = 0;
File files[];
indentLevel++;
files = path.listFiles();
for (int i=0, n=files.length; i < n; i++) {
for (int indent=0; indent < indentLevel; indent++) {
//Returns a float value for each file
common = (findCommonWords(firstFile(),readFile(files[i])));
//add all results of common
sum = new float[(int)files[i].length()];
sum[i] = (returnSimilarity(common, totalF1(), totalF2(files[i])));
}
if (files[i].isDirectory()) {
ReadDir(files[i]);
}
}
indentLevel--;
}
Here above sum will contain the values for every file in every directory instead of just the values for the files in the current directory. Does anybody know how to change this so as just the values for the current directory end up in sum. I would be grateful for any help you can give
Thanks
Last edited by cupanTae; 01-31-2006 at 02:51 PM.
-
Be sure the path passed in is the current directory, and take out the recursive call to ReadDir(files[i]);
-
ur code
import java.io.*;
/**
* Command line program to count lines, words and characters
* in files or from standard input, similar to the wc
* utility.
* Run like that: java WordCount FILE1 FILE2 ... or
* like that: java WordCount < FILENAME.
* @author Marco Schmidt
*/
public class WordCount {
/**
* Count lines, words and characters in given input stream
* and print stream name and those numbers to standard output.
* @param name name of input source
* @param input stream to be processed
* @throws IOException if there were I/O errors
*/
private static void count(String name, BufferedReader in) throws
IOException {
long numLines = 0;
long numWords = 0;
long numChars = 0;
String line;
do {
line = in.readLine();
if (line != null)
{
numLines++;
numChars += line.length();
numWords += countWords(line);
}
}
while (line != null);
System.out.println(name + "\t" + numLines + "\t" +
numWords + "\t" + numChars);
}
/**
* Open file, count its words, lines and characters
* and print them to standard output.
* @param fileName name of file to be processed
*/
private static void count(String fileName) {
BufferedReader in = null;
try {
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
count(fileName, in);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
/**
* Count words, lines and characters of given input stream
* and print them to standard output.
* @param streamName name of input stream (to print it to stdout)
* @param input InputStream to read from
*/
private static void count(String streamName, InputStream input) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(input);
BufferedReader in = new BufferedReader(inputStreamReader);
count(streamName, in);
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Determine the number of words in the argument line.
* @param line String to be examined, must be non-null
* @return number of words, 0 or higher
*/
private static long countWords(String line)
{
long numWords = 0;
int index = 0;
boolean prevWhitespace = true;
while (index < line.length()) {
char c = line.charAt(index++);
boolean currWhitespace = Character.isWhitespace(c);
if (prevWhitespace && !currWhitespace) {
numWords++;
}
prevWhitespace = currWhitespace;
}
return numWords;
}
public static void main(String[] args) {
if (args.length == 0) {
count("stdin", System.in);
} else {
for (int i = 0; i < args.length; i++) {
count(args[i]);
}
}
}
}
Similar Threads
-
By Arjuna in forum Database
Replies: 2
Last Post: 07-25-2007, 03:22 AM
-
By crazyguitarchik in forum Java
Replies: 1
Last Post: 11-30-2005, 08:12 PM
-
By sandhyaharsh in forum Java
Replies: 0
Last Post: 11-15-2005, 06:23 AM
-
By JohnN in forum VB Classic
Replies: 12
Last Post: 05-08-2001, 04:25 PM
-
By newToJava in forum Java
Replies: 2
Last Post: 03-07-2001, 08:25 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks