Finding and Counting Words in a File

Input File: OrdinaryMiracles

                   Ordinary Miracles
		   
		   by Abrams & Abrams
		   
Change can come on tiptoe. Love is where it starts. 
It resides, often hides deep within our hearts.
And just as pebbles make a mountain, raindrops make a sea, 
One day at a time change begins with you and me.

Ordinary miracles happen all around. Just by giving and receiving  
Comes belonging and believing.Every sun that rises never rose before. 
Each new day leads the way through a different door. 
And we can all be quiet heroes living quiet days, 
Walking through the world Changing it in quiet ways.

Ordinary miracles like candles in the dark. Each and every one of us 
Lights a spark, and the walls can tumble and the mountains can move. 
The winds and the tide can turn.

Ordinary miracles one for every star, no lightning bolt or 
Clap of thunder, only joy and quiet wonder, endless possibilities 
Right before our eyes. See the way a miracle multiplies, now 
Hope can spring eternally.Plant it and it grows, 
Love is all that's necessary.Love in its extraordinary way,  
Makes ordinary miracles every blessed day.






















Word Finder Example

import java.io.*;
import java.util.*;
/**
 * Look for a "word" in a file
 */
public class WordFinder {

  private boolean foundWord;
  
  private BufferedReader br;
  
  public WordFinder(String filename, String word) {
  
    foundWord = false;
    
    try { 
    
      br = new BufferedReader(new FileReader(filename));
      
      String line;
      
      while (!foundWord && (line = br.readLine()) != null) {
      
        StringTokenizer tokenizer = new StringTokenizer(line);
	
	while (!foundWord && tokenizer.hasMoreTokens()) {
	
	  String token = tokenizer.nextToken();
	  
	  if (token.equals(word))
	  
	    foundWord = true;
	}
      }
      
      br.close();
    }
    
    catch (IOException e) {
    
      System.err.println("File error: " + e);
      System.exit(1);
    }
  }
  
  boolean wordInFile() {
    return foundWord;
  }





  
  public static void main(String[] args) {
  
    WordFinder finder = new WordFinder("OrdinaryMiracles", "pebbles");
    if (finder.wordInFile())
      System.out.println("The word \"pebbles\" is in the file ");
    else
      System.out.println("The word \"pebbles\" is NOT in the file");
		       
    finder = new WordFinder("OrdinaryMiracles", "cat");
    if (finder.wordInFile())
      System.out.println("The word \"cat\" is in the file");
    else
      System.out.println("The word \"cat\" is NOT in the file");
  }
}
/*OUTPUT:
csc% java WordFinder
The word "pebbles" is in the file 
The word "cat" is NOT in the file
*/




































Word Counter Example

import java.io.*;
import java.util.*;
/**
 * Counts the number of times a given word appears in a given file
 */
public class WordCounter {

  private int wordCount;
  
  private BufferedReader br;
  
  public WordCounter(String filename, String word) {
  
    wordCount = 0;
    
    try { 
    
      br = new BufferedReader(new FileReader(filename));
      
      String line;
      
      while ((line = br.readLine()) != null) {
      
        StringTokenizer tokenizer = new StringTokenizer(line);
	
	while (tokenizer.hasMoreTokens()) {
	
	  String token = tokenizer.nextToken();
	  
	  if (token.equals(word))
	  
	    wordCount++;
	}
      }
      
      br.close();
    }
    
    catch (IOException e) {
    
      System.err.println("File error: " + e);
      System.exit(1);
    }
  }
  int getWordCount() {
  
    return wordCount;
  }
 
 




 
  
  public static void main(String[] args) {
  
    WordCounter counter = new WordCounter("OrdinaryMiracles", "miracles");
    System.out.println("The word \"miracles\" appears " +
                       counter.getWordCount() + " time(s).");
		       
    counter = new WordCounter("OrdinaryMiracles", "the");	       
    System.out.println("The word \"the\" appears " +
                       counter.getWordCount() + " time(s).");
		       
  
		       
  }
}

/*OUTPUT:
csc% java WordCounter
The word "miracles" appears 4 time(s).
The word "the" appears 7 time(s).
*/