// Helene Martin, CSE 142 // Counts the number of words on each line and reports the total words in the file. import java.io.*; import java.util.*; public class LineCounter { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("jabberwocky.txt")); int totalWords = 0; int lineCount = 0; while (input.hasNextLine()) { String line = input.nextLine(); lineCount++; int words = countLineWords(line); System.out.println("Words on line " + lineCount + ": " + words); totalWords = totalWords + words; } System.out.println("Words in file: " + totalWords); } // Counts and returns the number of words on the given line. public static int countLineWords(String line) { Scanner lineScan = new Scanner(line); // Scanner lineScan2 = new Scanner("hello there what's up?"); int lineWords = 0; while (lineScan.hasNext()) { lineWords++; lineScan.next(); } return lineWords; } }