number occurrances in a text file using indexOf


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: number occurrances in a text file using indexOf

  1. #1
    Join Date
    Aug 2006
    Posts
    1

    number occurrances in a text file using indexOf

    hiya please help, i am a beginner and i am trying to work out 1. how to count the number of occurrances of a specified word in a text file and 2.to count how many lines the specified word appears on. below is my code so far...

    Code:
    public class WordCounter1 {
    public static void main(String[] args) throws IOException {
    
    
    try{
    // input file
    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    
    
    int count = 0;
    int lines = 0;
    
    
    
    String line = in.readLine().toUpperCase(); // prime the pump for "while"
    
    while (line!=null) {
    
    // Tokenizer setup
    StringTokenizer wordFinder = new StringTokenizer(line);
    while (wordFinder.hasMoreTokens()) {
    String token = (wordFinder.nextToken());
    int n = args[1].indexOf(token);
    
    
    if (args[1].equalsIgnoreCase(token))
    count++;
    
    if (args[1] IS ON THE LINE?????
    lines++;
    
    
    
    }
    
    line = in.readLine();
    
    
    
    }
    
    System.out.println("There were " + count + " occurences");
    System.out.println("The word " + args[1] + " is on " + lines + " lines");
    
    
    in.close();
    
    
    
    
    }
    
    
    catch (FileNotFoundException e) {
    System.out.println("File opening problem.");
    }
    
    
    }
    }

  2. #2
    Join Date
    Aug 2006
    Posts
    7
    i don't use tokenizers all that much, but if you're using java 1.5.xxx I'd look into using the Scanner class. with scanner you can read the file line by line and it would be much simpler in my opinion. Also once you get each line in as a string, you can use indexOf(String, int) inside of a while loop to get how many times, etc. the string appears in the document you're reading.

  3. #3
    Join Date
    Dec 2005
    Posts
    97
    may i ask why you are using StringTokenizer? the way i wrote this was just using two while loops (one nested inside the other). the outer loop determined how many lines the word was on and the inner loop found the number of occurances. i just used indexOf(String, int) and indexOf(String) to find these values.

Similar Threads

  1. Importing text file using schema.ini
    By Kevin in forum VB Classic
    Replies: 3
    Last Post: 12-05-2005, 06:25 PM
  2. Replies: 0
    Last Post: 08-08-2002, 05:56 AM
  3. XML to Text File using CopyBook
    By Nischal in forum XML
    Replies: 0
    Last Post: 03-21-2002, 06:10 PM
  4. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  5. Replies: 2
    Last Post: 05-31-2000, 10:11 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