Question: 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

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 words in case-insensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output in alphabetical order of words, with each word preceded by its occurrence count 


Data from Listing 21.9

1mport java. util.: 2 1 3 publ1c class CountOccurrence0fWords { publ1c stat1c

void main(String[] args) { I/ Set text in a string String text

1mport java. util.: 2 1 3 publ1c class CountOccurrence0fWords { publ1c stat1c void main(String[] args) { I/ Set text in a string String text = "Good morn1ng. Have a good class. " + "Have a good visit. Have fun!"; 4 6 7 8 II Create a TreeMap to hold words as key and count as value Map map = new TreeMapo (): 10 11 12 String[] words = text.split("[Is+1 \p{P}]"): for (1nt i = 0: i < words.length: i++) { String key = words [i].tolowerCase (): 13 14 15 1f (key. length () > 0) { 1f (Imap.containsKey (key)) { map. put (key. 1): 16 17 18 19 else { int value = map.get (key): 20 21 22 value++; 23 map. put (key, value): 24 25 26 27 28 II Display key and value for each entry

Step by Step Solution

3.40 Rating (163 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javautil import javaio public class Exercise2108 public static void mainString args if argsle... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction to Java Programming and Data Structure Questions!