C:\Users\notebook\Documents\NetBeansProjects\CS2\src\applications\FrequencyCounter2.java |
1
2
3
4
5
6
7
8
9
10
11
12 package applications;
13
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.util.HashMap;
17 import java.util.Scanner;
18
19
20
21 @author
22
23 public class FrequencyCounter2 {
24
25
26 @param args
27
28 public static void main(String[] args) throws FileNotFoundException {
29 HashMap<String, Integer> counter = establishFrequencyCounter();
30 dump(counter);
31 interpret(counter);
32
33 }
34
35 private static HashMap<String, Integer> establishFrequencyCounter() throws FileNotFoundException {
36 Scanner scanner = equateScannerWithFile();
37 HashMap<String, Integer> counter = new HashMap<>();
38 while (scanner.hasNext()) {
39 String key = scanner.next().toLowerCase();
40 if (counter.containsKey(key)) {
41 counter.replace(key, counter.get(key), counter.get(key) + 1);
42 } else {
43 counter.put(key, 1);
44 }
45 }
46 return counter;
47 }
48
49 private static void dump(HashMap<String, Integer> counter) {
50 System.out.println("--- Begin contents of the hash map ...");
51 for (String key : counter.keySet()) {
52 System.out.println("<" + key + ":" + counter.get(key) + ">");
53 }
54 System.out.println("--- End contents of the hash map ...");
55 }
56
57 private static void interpret(HashMap<String, Integer> counter) {
58 Scanner scanner = new Scanner(System.in);
59 while (true) {
60 System.out.print(">>> ");
61 String command = scanner.nextLine();
62 if (command.equalsIgnoreCase("count")) {
63 interpretCount(scanner, counter);
64 } else if (command.equalsIgnoreCase("print")) {
65 interpretPrint(counter);
66 } else if (command.equalsIgnoreCase("exit")) {
67 break;
68 } else if (command.equalsIgnoreCase("help")) {
69 interpretHelp();
70 } else {
71 System.out.println(
72 "Sorry, the command you entered is not available. "
73 + "You can use the \"HELP\" command to see all commands available.");
74 }
75 }
76 }
77
78 private static Scanner equateScannerWithFile() throws FileNotFoundException {
79 System.out.print("Enter file name here: ");
80 Scanner s = new Scanner(System.in);
81 String fileName = s.nextLine();
82 String FullFileName = createFullFileName(fileName);
83 return new Scanner(new File(FullFileName));
84 }
85
86 private static String createFullFileName(String fileName) {
87 String separator = System.getProperty("file.separator");
88 String home = System.getProperty("user.home");
89 String path = home + separator + "Documents" + separator + "NetBeansProjects" + separator;
90 String fullFileName = path + fileName;
91 return fullFileName;
92 }
93
94 private static void interpretCount(Scanner scanner, HashMap<String, Integer> counter) {
95 System.out.print("word? ");
96 String word = scanner.nextLine().toLowerCase();
97 if (counter.containsKey(word)) {
98 System.out.println(counter.get(word));
99 } else {
100 System.out.println(word + " is not in the hash map.");
101 }
102 }
103
104 private static void interpretPrint(HashMap<String, Integer> counter) {
105 for (String key : counter.keySet()) {
106 System.out.println("<" + key + ":" + counter.get(key) + ">");
107 }
108 }
109
110 private static void interpretHelp() {
111 System.out.println("COUNT -- Give the frequency of occurrence of the word entered");
112 System.out.println("PRINT -- Display the hash map");
113 System.out.println("EXIT -- Exit the program");
114 System.out.println("HELP -- Display all available commands");
115 }
116
117 }
118