Advanced WordCounter help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Advanced WordCounter help

Threaded View

  1. #1
    Join Date
    Aug 2005
    Posts
    6

    Advanced WordCounter help

    hi,

    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:
    e.g
    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:

    Code:
    import java.io.*;
    import java.util.*;
    
    class WordCounter {
    
    // Main
    
    public static void main (String[] args) {
    WordCounter t = new WordCounter();
    t.fileRead();
    }
    
    
    // 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) {
    numLines++;
    
    }
    
    
    // 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!");
    e.printStackTrace();
    }
    
    } // End of fileRead
    
    } // End of class
    Thanks if anyone can help me with this it's been annoying me now for a long time.

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

Similar Threads

  1. Replies: 0
    Last Post: 02-25-2005, 11:21 AM
  2. Advanced .NET Testing System - ANTS
    By Red-Gate in forum dotnet.announcements
    Replies: 0
    Last Post: 12-16-2002, 11: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, 11: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, 09:08 AM
  5. Advanced DCOM Trainer
    By Zubair in forum Enterprise
    Replies: 1
    Last Post: 05-16-2000, 08: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
 
 
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