// This is file wc.java. It implements a simple word-count program to // illustrate the use of the Java library class Scanner for reading from // terminal input. // import java.io.*; import java.util.*; public class wc { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numWords=0; int numLines=0; String line; String word; while (sc.hasNextLine()) { line = sc.nextLine(); // read a line numLines++; Scanner sw=new Scanner(line); // scan through the line while (sw.hasNext()) { word = sw.next(); // take a token off the line numWords++; } // end loop over words } System.out.println("Words: " + numWords + " Lines: " + numLines); } }