[JAVA] ( Count the occurrences of words in a text file ) Rewrite Listing 21.9 to
ID: 3726166 • Letter: #
Question
[JAVA]
(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.
I want to be able to enter a file name manully when running the program(NO CMD) and it should work correctly. Must use set/maps not simple code or different code.
import java.util.*;
import java.io.*;
public class WordCounter {
public static void main(String[] args){
//String fileName = args[0];
TreeMap<String,Integer> treeMap=new TreeMap<String,Integer>();
int wordCount = 0;
try{
Scanner input = new Scanner(new File(fileName));
while (input.hasNextLine()){
String line=input.nextLine();
String[]words=line.split("[\s+\p{P}]");
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: "+wordCount);
for (Map.Entry<String, Integer>entry: entrySet) {
System.out.println(entry.getValue()+" " +entry.getKey());
}
}
}
Explanation / Answer
Explanation:
Please go through below updated WordCounter class and revert back in case anything else needs to be change.
Program:
import java.util.*;
import java.io.*;
public class WordCounter {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter input file name: ");
String fileName = userInput.nextLine();
TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>();
int wordCount = 0;
try {
Scanner input = new Scanner(new File(fileName));
while (input.hasNextLine()) {
String line = input.nextLine();
String[] words = line.split("[\s+\p{P}]");
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);
}
}
}
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
Set<Map.Entry<String, Integer>> entrySet = treeMap.entrySet();
System.out.println(" Total words: " + wordCount);
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getValue() + " " + entry.getKey());
}
userInput.close();
}
}
Output:
Enter input file name: sample.txt
Total words: 97
3 a
1 about
2 account
1 actually
1 already
2 and
1 be
1 because
2 being
2 by
1 card
1 claiming
1 clicked
1 code
1 contacted
1 counted
1 credit
4 ebay
1 email
1 emails
1 from
1 genuine
1 going
1 had
1 he
1 html
1 in
2 information
1 into
1 is
1 it
1 legitimate
1 like
1 link
1 look
1 make
1 mimicking
1 of
2 on
1 organization
1 people
1 phishing
1 proliferation
1 provided
1 received
1 relatively
3 s
1 saw
2 scam
1 simple
2 site
1 subsequently
1 supposedly
1 suspended
2 that
7 the
1 their
1 they
1 thinking
4 to
1 tricked
1 unless
1 update
1 updated
1 user
1 users
1 was
1 website
2 were
1 which
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.