COP 3337 SECTION U08 FALL 2015 COUNTING THE'S ============== I. This program reads a text file and prints out the number of occurrences of the string "the" in the file. The program: import java.io.*; import java.util.Scanner; public class CountThe { public static void main(String[] args) { // form a scanner for the keyboard Scanner keyboard = new Scanner(System.in); // get the name of the input file Scanner in = null; // the scanner for the file String name = null ; // the name of the file try { System.out.print("Enter the name of the input file >"); name = keyboard.next(); File inFile = new File(name); in = new Scanner(inFile); } catch (IOException e) { System.err.println("Error. The file cannot be opened."); System.exit(1); } System.out.println("The Number of The's in the File"); System.out.println("===============================\n\n"); // process the input file int count = 0; // the number of the's in the file while(in.hasNext()) { // the file is not empty, get the next word String current = in.next(); if ( current.equalsIgnoreCase("the")) count++; } System.out.println("There are " + count + " the's in file " + name + "."); System.exit(0); } } II. The input file mosh.txt is between the 2 dash lines. -------------------------------------------------------------- The old man had a dog. The dog's name was Jeff. Their house was at the corner of Nowhere Street. -------------------------------------------------------------- III. The output: run: Enter the name of the input file >U:\mosh.txt The Number of The's in the File =============================== There are 3 the's in file U:\mosh.txt. BUILD SUCCESSFUL (total time: 33 seconds)