// Zorah Fung, CSE142 // An example of using line and token based processing. // Counts and prints out the number of words of each line of a file // Note: We can send the results to the console or a file using // Printstream import java.io.*; import java.util.*; public class WordCounter { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("input.txt")); // Printstream out = new PrintStream("output.txt"); int lineNumber = 0; while (input.hasNextLine()) { String line = input.nextLine(); lineNumber++; Scanner tokens = new Scanner(line); // process the contents of this line int count = 0; while (tokens.hasNext()) { String word = tokens.next(); count++; } System.out.println("Line #" + lineNumber + " has " + count + " words"); // out.println("Line has " + count + " words"); } } }