/*  CSI2114 Lab 3 - search.java
 * 
 *  counts the number of occurrences of a word in a tet file.
 *
 *  Usage: java search <word> <file>
 * 
 *  by Jeff Souza 
 *
 */

import java.io.*;
import java.util.*;

public class search {
   
    public static void main(String[] args) throws IOException {

      if (args.length == 2) {

        BufferedReader in;              			// input stream
        String line;                    			// current line
        String word;                    			// current word
        int count = 0;                   			// count # of occurrences of word

        // opens input stream for reading
        in = new BufferedReader(new FileReader(args[1]));
       
        line = in.readLine();
        while (line != null) {
            StringTokenizer st = new StringTokenizer(line);
            while (st.hasMoreTokens()) {
                word = st.nextToken();
                if (word.toLowerCase().compareTo(args[0].toLowerCase()) == 0) count++;  
            }
            line = in.readLine();
        }
 
        System.out.println("The word '" + args[0].toLowerCase() + "' appears " + count + " times in the file " + args[1] + ".");
      }
      else { 
            System.out.println("Usage: java search <word> <file>"); 
      } 
    } 
}