home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Documents / JAVA Programming / examples / 06 / WordCount.java < prev   
Encoding:
Java Source  |  2000-09-08  |  935 b   |  29 lines

  1. class WordCount {
  2. static String text = "Now is the time\n" + 
  3.                      "for all good men\n" +
  4.                      "to come to the aid\n" +
  5.                      "of their country\n"+
  6.                      "and pay their due taxes\n";
  7. static int len = text.length();
  8. public static void main(String args[]) { 
  9. boolean inWord = false;
  10. int numChars = 0;
  11. int numWords = 0;
  12. int numLines = 0;
  13. for (int i=0; i < len; i++) { 
  14.      char ß = text.charAt(i);
  15.      numChars++;
  16.      switch (ß) { 
  17.              case '\n': numLines++; // FALLSTHROUGH 
  18.              case '\t': // FALLSTHROUGH 
  19.              case ' ' : if (inWord) { 
  20.                             numWords++;
  21.                             inWord = false;
  22.                         } 
  23.                         break;
  24.             default: inWord = true;
  25.                 } 
  26.      }
  27. System.out.println("\t" + numLines +"\t" + numWords + "\t" + numChars);
  28. } }
  29.