DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Threaded View

  1. #1
    Join Date
    Aug 2005

    Advanced WordCounter help


    I've worked on doing an application to read a txt file, then print back the number of characters, words and the number of lines in the text file. Once thats been done the application must then be able to output the number of occurences of each word in the text file and on how many lines each word is:
    the quick brow fox jumped over the
    lazy dog
    This would give:
    2 occurences of "the"
    1 line contains "the"

    I'm new to java and i've managed to get the number of lines to report back but i'm not sure on how to get the rest done. I've explored using the StringTokenizer method and String.indexOf method to achieve my results but i'm finding it very difficult to code. If anyone has a chance to take a look here's the code i've been able to do:

    import java.io.*;
    import java.util.*;
    class WordCounter {
    // Main
    public static void main (String[] args) {
    WordCounter t = new WordCounter();
    // Read the file and output
    void fileRead() {
    String record = null;
    int numLines = 0;
    int numWords = 0;
    int numChars = 0;
    try {
    FileReader fr = new FileReader("test.txt");
    BufferedReader br = new BufferedReader(fr);
    record = new String();
    while ((record = br.readLine()) != null) {
    // Output values
    System.out.println("Number of lines:" + numLines);
    System.out.println("Number of words:" + numWords);
    System.out.println("Number of chars:" + numChars);
    } catch (IOException e) {
    // Catch possible io errors from readLine()
    System.out.println("IOException error!");
    } // End of fileRead
    } // End of class
    Thanks if anyone can help me with this it's been annoying me now for a long time.

    Last edited by jarvio678; 08-11-2005 at 08:43 AM.

Similar Threads

  1. Replies: 0
    Last Post: 02-25-2005, 10:21 AM
  2. Advanced .NET Testing System - ANTS
    By Red-Gate in forum dotnet.announcements
    Replies: 0
    Last Post: 12-16-2002, 10:23 AM
  3. ANN: Archetype, advanced OO and design patterns tool
    By John Hancock in forum vb.announcements
    Replies: 0
    Last Post: 02-05-2002, 10:46 PM
  4. Advanced VB5/6 Diagnostic Tools V1.2 Released
    By Geoff Kell in forum vb.announcements
    Replies: 0
    Last Post: 02-07-2001, 08:08 AM
  5. Advanced DCOM Trainer
    By Zubair in forum Enterprise
    Replies: 1
    Last Post: 05-16-2000, 07:21 AM

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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.