Rewrite Listing 21.9 to read the text from a text file. The text file is passed as
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 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.
Listing
Transcribed Image Text:
1 import java.util.*; 2 3 public class CountOccurrence0fWords { 4 public static void main(String[] args) { // Set text in a string String text = "Good morning. Have a good class. " + "Have a good visit. Have fun!"; 5 // Create a TreeMap to hold words as key and count as value Map
1 import java.util.*; 2 3 public class CountOccurrence0fWords { 4 public static void main(String[] args) { // Set text in a string String text = "Good morning. Have a good class. " + "Have a good visit. Have fun!"; 5 // Create a TreeMap to hold words as key and count as value Map map = new TreeMap<>(); 10 11 12 13 String[] words = text.split("[ \n\t\r.,;:!?(){}]"); for (int i = 0; i < words.length; i++) { String key = words[i].toLowerCase(); 14 |15 if (key.length() > 0) { if (!map.containskey(key)) { map.put(key, 1); 16 |17 18 19 20 21 22 23 24 25 26 27 28 29 30 else { int value = map.get(key); value++; map.put(key, value); // Get all entries into a set Set> entrySet = map.entrySet(); // Get key and value from each entry for (Map.Entry entry: entrySet) System.out.println(entry.getKey() + "\t" + entry.getValue()); 31 32 33 34 35 } a 2 class fun good have 3 morning visit
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (7 reviews)
Program Plan Step 1 Reads the text from the text file ...View the full answer
Answered By
Munir Ahmed Jakhro
I am professional Tutor of of Business Courses, I did my four years Bachelor Degree from one of the Top Business schools of World "Institute of Business Administration" in year 2013. Since then I have been working as Tutor of Accounting, Finance tutor on different online platforms like this website. I am have experience of 6 years teaching business courses to students online and offline my professional job at national savings also helped me in accounting understanding .
4.90+
8+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Rewrite Listing 2.10, ComputeChange. java, to fix the possible loss of accuracy when converting a float value to an int value. Read the input as a string such as "11.56". Your program should extract...
-
CountOccurrenceOfWords.java) Rewrite Listing 21.9 to display the words in ascending order of occurrence counts. Listing 1 import java.util.*; 2 3 public class CountOccurrence0fWords { 4 public static...
-
Rewrite Listing 21.7 CountKeywords. java to read in a Java source code file and count the occurrence of each keyword in the file, but don?t count the keyword if it is in a comment or in a string...
-
For nearly 50 years, Loganville Window Treatments (LWT) of Loganville, Georgia, has made interior shutters that are sold through decorating centers. Figure 3.20 shows some of the various styles of...
-
If S X is a subspace of a linear space X, then 1. S contains the null vector 0 2. for every x S, the inverse -x belongs to S
-
The Large Steel Company has $1.5 billion of current assets, $0.8 billion of current liabilities, and $1 billion of long-term debt. It generates $1 billion of funds per year. Compute some meaningful...
-
Reconsider Problem 65. Plot a graph of capital recovery cost versus useful life for lives 1 to 25 by 1. Data from problem 65 Crush Autosmashers can purchase a new electromagnet for moving cars at a...
-
Willow Brook National Bank operates a drive-up teller window that allows customers to complete bank transactions without getting out of their cars. On weekday mornings, arrivals to the drive-up...
-
Explain the FOUR ways you can map EERD to Relational Model?
-
Mr. LN. Vestor is retired and slowly selling some of his assets to supplement his retirement income. He wants to have some idea of how much tax he is going to owe for 2020 on the asset sales that...
-
Rewrite Programming Exercise 8.37? to store pairs of each state and its capital in a map. Your program should prompt the user to enter a state and should display the capital for the state. Write a...
-
Revise the program in Listing 21.7. If a keyword is in a comment or in a string, don?t count it. Pass the Java file name from the command line. Assume that the Java source code is correct and line...
-
Two stockbrokers are having a discussion. One claims that Netmark will introduce a new product if and only if both Datapro cuts its workforce and Compucel expands production. The other claims that...
-
Over the last 5 years, Steven made the following purchases of Aziz Inc. in his brokerage account: Year 1 - 200 shares @ $34; Year 2 - 150 shares @ $39; Year 3 - 100 shares @ $45 This year, he sold...
-
Two spheres; 4.0 cm apart, attract each other with a force of 1.2 x 10- N. Determine the magnitude of the charge on each, if one has twice the charge (of the opposite sign) as the other.
-
Determine the infinite limit. lim x+1 x0x2(x+2)
-
Michele goes on a hike that will last 5 hours total. She brings 8 cups of water. After 1 hour, she has already drunk 2 cups of water. At that rate, how many cups of water would Michele need for all 5...
-
A rule of thumb in the fast-food restaurant business is a "4 times markup." This means the price of a food item should be 4 times the price of the ingredients used in making the item. If the cost of...
-
You are the controller of Small Toys Inc. Mutahir Kazmi, the president, recently mentioned to you that he found an error in the 2013 financial statements that he believes has now corrected itself. In...
-
The Alert Company is a closely held investment-services group that has been very successful over the past five years, consistently providing most members of the top management group with 50% bonuses....
-
What is the bandwidth of a signal that can be decomposed into five sine waves with frequencies at 0, 20, 50, 100, and 200 Hz? All peak amplitudes are the same. Draw the bandwidth.
-
Name three types of transmission impairment.
-
What is the phase shift for the following? a. A sine wave with the maximum amplitude at time zero b. A sine wave with maximum amplitude after 1/4 cycle c. A sine wave with zero amplitude after 3/4...
-
As a precaution, the levers on gasoline pumps in many provinces in Canada must be manually operated at all times. One of the reasons for this is especially relevant in the winter time because the...
-
7. A transformer core is shown in cartoon form in Figure 8.26. This is made up of, perhaps implausibly, 29 gage M-36 laminations: the same sort of material described by Figures 8.19 and 8.20. Assume...
-
2. For a lamp that has a total luminous flux, of 4.5104 lm and total radiant flux, of 300 W, (a) what is the overall luminous efficiency, K for this lamp? (b) if this lamp has a luminous intensity of...
Study smarter with the SolutionInn App