// This program prompts the user for the name of a file and then counts the // occurrences of words in the file (ignoring case) and then prints the number // of times the word the appears. import java.util.*; import java.io.*; public class WordCount { // counts # of words // count # lines? // count # characters? // count # tale? // count # sh? // count unique words? set! // count the number of times each unique word appears? map! public static void main(String[] args) throws FileNotFoundException { // open the file Scanner console = new Scanner(System.in); System.out.print("What is the name of the text file? "); String fileName = console.nextLine(); Scanner input = new Scanner(new File(fileName)); Map words = new HashMap(); while (input.hasNext()) { String word = input.next(); if (!words.containsKey(word)) { words.put(word, 1); } else { int count = words.get(word); // the count of word words.put(word, count + 1); } } System.out.println("Number of UNIQUE words in " + fileName + ": " + words.size()); System.out.println("count of the: " + words.get("the")); } }