[JAVA] The code below works but it needs to be changed to follow these instructi
ID: 3725937 • Letter: #
Question
[JAVA]
The code below works but it needs to be changed to follow these instructions
(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 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.io.*;
import java.util.*;
public class CountWordsFile {
public static void main(String[] args){
//String javaprogramming = args[0];
TreeMap<String,Integer> treeMap=new TreeMap<String,Integer>();
int wordCount = 0;
try{
Scanner input = new Scanner(new File("testing.txt"));
while (input.hasNextLine()){
String line=input.nextLine();
String[]words=line.split("\s+");
wordCount = wordCount+words.length;
for (int i=0;i<words.length;i++){
if (words[i].trim().length () >0 && words[i].trim().matches("[A-Z|a-z]+")){
String key =words[i].toLowerCase();
if (treeMap.get(key)!=null){
int count =treeMap.get(key);
count++;
treeMap.put(key,count);
}
else{
treeMap.put(key, 1);
}
}
}
}
}
catch (Exception ex){
ex.printStackTrace();
}
Set<Map.Entry<String,Integer>>entrySet=treeMap.entrySet();
System.out.println(" Total words in the file: "+wordCount);
for (Map.Entry<String, Integer>entry: entrySet) {
System.out.println(entry.getValue()+" " +entry.getKey());
}
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class CountWordsFile{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the file path: ");
File file = new File(scanner.next());
if (!file.isFile()) {
System.out.println(file + " is not a file.");
}
String[] words;
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
StringBuilder buffer = new StringBuilder(10000);
String s;
while ((s = in.readLine()) != null)
buffer.append(s).append(" ");
words = buffer.toString().split("[0-9]+|\W+");
} catch (IOException ex) {
words = new String[1];
System.out.println("Error opening file...");
System.exit(0);
}
// To Create a TreeMap to hold words as key and count as value
Map<String, Integer> map = new TreeMap<>();
for (String word1 : words) {
String key = word1.toLowerCase();
if (key.length() > 0) {
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));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.