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
[1656 byte] By [
cupanTae] at [2007-11-11 7:53:31]

# 2 Re: Help with counting words in a file please
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]);
}
}
}
}