// This is file wcFile.java. It implements a simple word-count program to // illustrate the use of the Java library class Scanner for reading from // a text file. import java.io.*; import java.util.*; public class wcFile { public static void main(String[] args) { try { Scanner sc = new Scanner(new File("input.txt")); 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); } catch (FileNotFoundException fnfe) {} } }