Help with counting words in a file please


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help with counting words in a file please

  1. #1
    Join Date
    Oct 2005
    Posts
    16

    Arrow 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.

  2. #2
    Join Date
    Jan 2006
    Posts
    53
    Be sure the path passed in is the current directory, and take out the recursive call to ReadDir(files[i]);

  3. #3
    Join Date
    Jan 2007
    Posts
    11

    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

  1. Database not properly created
    By Arjuna in forum Database
    Replies: 2
    Last Post: 07-25-2007, 03:22 AM
  2. How to read all words in a text file
    By crazyguitarchik in forum Java
    Replies: 1
    Last Post: 11-30-2005, 08:12 PM
  3. upload an image file and save on webserver.
    By sandhyaharsh in forum Java
    Replies: 0
    Last Post: 11-15-2005, 06:23 AM
  4. Test a file to see if it's open
    By JohnN in forum VB Classic
    Replies: 12
    Last Post: 05-08-2001, 04:25 PM
  5. 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center