Source of CountWords.java


  1: import java.util.*; 
  2: import java.io.*; 
  3: 
  4: public class CountWords {
  5:   static public void main(String[] args) {
  6:     HashSet words = new HashSet(); 
  7:     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
  8:     String delim = " \t\n.,:;?!-/()[]\"\'";
  9:     String line; 
 10:     int count = 0; 
 11:     try {
 12:       while ((line = in.readLine()) != null) {
 13:         StringTokenizer st = new StringTokenizer(line, delim); 
 14:         while (st.hasMoreTokens()) {
 15:           count++; 
 16:           words.add(st.nextToken().toLowerCase()); 
 17:         }
 18:       } 
 19:     } catch (IOException e) {}
 20:     
 21:     System.out.println("Total number of words: " + count); 
 22:     System.out.println("Number of different words: " + words.size()); 
 23:   }
 24: }