-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWordCountHashMap.java
More file actions
130 lines (99 loc) · 4.28 KB
/
FileWordCountHashMap.java
File metadata and controls
130 lines (99 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Sukhamrit Singh
// FileWordCountHashMap
/*
This program reads the text from a text file. The text file is passed
as a command-line argument. Words are delimited by white space
characters, punctuation marks (, ; . : ?), quotation marks (' "), and
parentheses. Count the words in a case-sensitive fashion (e.g., consider
Good and good to be the same word). The words must start with a letter.
Display the output of words in alphabetical order, with each word
preceded by the number of times it occurs.
*/
import java.util.*;
import java.io.*;
public class FileWordCountHashMap {
public static void main(String[] args) throws Exception {
// Check if user specified file name in the command line.
// only one argument should be specified
if (args.length != 1) {
System.out.println("");
System.out.println("Usage: java -classpath <classpath> " +
"FileWordCountHashMap <textfile>");
System.out.println("");
System.exit(1);
}
// first argument is the file name
String fileName = args[0];
// Check if the specified text file exists
File textFile = new File(fileName);
if (!textFile.exists()) {
System.out.println("The textfile " + fileName +
" does not exists.");
System.exit(1);
}
// Create a TreeMap to hold words as key and value (word count)
SortedMap<String, Integer> wordsMap = new TreeMap<>();
try (
// read the file contents
Scanner input = new Scanner(textFile);
) {
// read file one line at a time
while ( input.hasNext() ) {
// read next line
String line = input.nextLine();
// split the line into words using space, comma,
// semi-colon etc as delimeters
String[] lineWords = line.split("[ @!~{}\\[\\]$#^&*\n\t\r.,;?'\")(]");
// calling method to add words to the hash map
storeWord(wordsMap, lineWords);
}
}
// Print the words in alphabetical order
for( Map.Entry<String,Integer> entry : wordsMap.entrySet() ) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(value + "\t" + key);
}
}
// Method to find and store the words with their occurance count
private static void storeWord(Map<String, Integer> wordsMap,
String[] words) {
// for every word in the words array
for (int i = 0; i < words.length; i++) {
// Making sure that the words only contain letters,
// no special characters
if (words[i].trim().length() > 0 &&
words[i].trim().matches("[A-Z|a-z]+")) {
// covert to lowercase for case in-sensitive comparison
String entry = words[i].toLowerCase();
// ignore word with 0 length
if ( entry.length() == 0 ) {
continue; // go to the next entry in the array
}
// check if the word starts with a letter
boolean startsWithLetter =
Character.isLetter(entry.charAt(0));
// ignore word that does not start with a letter
if ( !startsWithLetter ) {
continue; // go to next entry in the array
}
// check if the entry alreay exist in hash map
boolean entryExists = wordsMap.containsKey(entry);
if ( entryExists) {
// if word entry exist, then get the current count
int count = wordsMap.get(entry);
// increment the count
count++;
// update the word entry count in the hashmap
wordsMap.put(entry, count);
} else {
// the word does not exist in the hash map
// set the count to 1
int count = 1;
// add the word entry to hash map
wordsMap.put(entry, count);
}
}
}
}
}