// WordCount.java -- simple program counting lines, words, and chars class WordCount { static final String text = """ Now is the time for all good men to come to the aid of their country and pay their due taxes """; static final int len = text.length(); static public void main (String args[]) { boolean inWord = false; int numChars = 0; int numWords = 0; int numLines = 0; for (int i = 0; i<len; i++) { final char c = text.charAt(i); numChars++; switch (c) { case '\n': numLines++; // FALLSTHROUGH case '\t': case ' ': if (inWord) { numWords++; inWord = false; } break; default: inWord = true; } } System.out.println ("%5d %5d %5d%n", numLines, numWords, numChars); } }