Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the
ID: 3590480 • Letter: C
Question
Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by whitespace characters punctuation marks (,?), quotation mark:s ("), 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.Explanation / Answer
// Count_the_occurrences.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class Count_the_occurrences {
public static void main(String[] args) {
validateArgs(args, 1);
String text = getTextFromFilePath(args[0]);
Map<String, Integer> map = new TreeMap<>();
String[] words = text.split("[\s+\p{P}]");
for (String word : words) {
String key = word.toLowerCase();
if (key.matches("[a-z].*")) {
if (!map.containsKey(key)) {
map.put(key, 1);
} else {
int value = map.get(key);
value++;
map.put(key, value);
}
}
}
map.forEach((k, v) -> System.out.println(k + " " + v));
}
private static String getTextFromFilePath(String filePath) {
Path path = Paths.get(filePath);
byte[] bytes = new byte[0];
try {
bytes = Files.readAllBytes(path);
} catch (IOException e) {
e.printStackTrace();
}
return new String(bytes);
}
private static void validateArgs(String[] args, int expectedLength) {
if (args.length != expectedLength) {
System.out.println("Wrong number of arguments.");
System.exit(1);
}
}
}