Modify the program of Fig. 17.22 to summarize the number of occurrences of every character in the
Question:
Modify the program of Fig. 17.22 to summarize the number of occurrences of every character in the file.
Fig. 17.22
Transcribed Image Text:
1 // Fig. 17.22: StreamOfLines.java 2 // Counting word occurrences in a text file. 4 3 import java.io.IOException; import java.nio.file.Files; 5 import java.nio.file. Paths; 6 import java.util.Map; 7 import java.util.TreeMap; 8 import 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 wwwww N N N N 29 30 31 32 33 java.util.regex.Pattern; import java.util.stream.Collectors; public class StreamOfLines { public static void main(String[] args) throws IOException { // Regex that matches one or more consecutive whitespace characters Pattern pattern = Pattern.compile("\\s+"); 34 35 } } // count occurrences of each word in a Stream sorted by word Map wordCounts = Files.lines (Paths.get("Chapter 2Paragraph. txt")) .flatMap (line -> pattern.splitAsStream (line)) .collect (Collectors.grouping By (String :: toLowerCase, TreeMap::new, Collectors.counting (())); // display the words grouped by starting letter wordCounts.entrySet() .stream() .collect( Collectors.grouping By (entry -> entry.getKey().charAt(0), TreeMap:: new, Collectors.toList())) .forEach((letter, wordList) -> { System.out.printf("%n%C%n", letter); wordList.stream().forEach (word -> System.out.printf( "%13s: %d%n", word.getKey(), word.getValue())); });
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 33% (3 reviews)
The current program in the image is designed to count the occurrences of each word in a text file an...View the full answer
Answered By
Wonder Dzidzormenu
As a professional accountant and a teacher, I explain account ing concepts in a more practical way that makes students more connected to the subject.
With over 10 years of teaching accounting , I offer a well constructed , easily understood and in-depth explanations to students questions.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
You are required to write a Python program that will manage character (heroes and villain) information. Character (hero and villain) information will be stored in a text file that will be read in...
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
Modify the program of Table 4.1 to find the normalized wave function.
-
2 3 -4 6,-12,-18 If A = (a) 0 3a 2b 24 (b) -6,4,9 and KA= then the value of k, a,b are respectively (c) -6,-4,-9. (d) -6,12,18
-
For each item below, indicate to which category of elements of financial statements it belongs. (a) Retained earnings (b) Sales (c) Additional paid-in capital (d) Inventory (e) Depreciation (f) Loss...
-
WRT owns a chain of retail bookstores. It recently decided to add coffee bars in each store to sell gourmet coffee drinks and pastries to the bookstore customers. WRT has not yet obtained the...
-
It has been proposed that the United States should strive toward a hydrogen economy. At present, the United States uses about 100 quads \(\left(10^{17} \mathrm{Btu} ight)\) of energy per year....
-
Trichloroethylene, a widely used degreasing solvent for machine parts is produced in a two-step reaction sequence. Ethylene is first chlorinated to yield tetrachloroethane, which is...
-
Patricia McDonald has determined that the value of her liquidassets is $4,200, the value of her real estate is $134,000, thevalue of her personal possessions is $55,000, and the value of...
-
How to get informationabout files and directories on disk. In addition, you used a DirectoryStream to display the contents of a directory. Interface DirectoryStream now contains default method...
-
Assuming that list is a List , explain in detail the stream pipeline: 1234 4 list.stream() .filter(value .distinct() .count() -> value < 0)
-
An Allowance for Doubtful Debts is created (A) When debtors become bankrupt (B) When debtors cease to be in business (C) To provide for possible bad debts (D) To write-off bad debts
-
An inductor for which \(L=10 \mathrm{mH}\) is connected to an \(\mathrm{AC}\) source. The source emf is given by \(\mathscr{E}=\mathscr{E}_{\max } \sin \omega t\), with \(\mathscr{E}_{\max }=5.0...
-
Unlike electrons, the holes in a semiconductor do not leave the semiconductor in which they are formed. Yet, holes can be thought of as positive charge carriers. When a hole has traveled to the edge...
-
You must use a resistor, a capacitor, or an inductor to fill a gap in a circuit. You want to have as much current as possible through this circuit element to ensure that it can be easily detected....
-
A sample contains \(3.0 \times 10^{22} \mathrm{Si}\) atoms and nothing else. (a) How many valence electrons does the sample contain? (b) How many of the valence electrons in the sample can conduct...
-
The research laboratory you work in is less than a block away from the transmitting antenna of a radio station that broadcasts at \(1100 \mathrm{kHz}\). You notice that every piece of equipment in...
-
Repeat Prob. 668, with the bar subject to a completely reversed torsional moment of 2000 lbf in. Repeat Prob. 668, A 1 -in-diameter hot-rolled steel bar has a 3/16 -in diameter hole drilled...
-
In Exercises 15 through 30, find the derivative dy/dx. In some of these problems, you may need to use implicit differentiation or logarithmic differentiation. y ex + et -2x 1 + e
-
Byte-stuff the following frame payload in which E is the escape byte, F is the flag byte, and D is a data byte other than an escape or a flag character. DEDDF D D E|ED F|D
-
Explain why flags are needed when we use variable-size frames.
-
Unstuff the following frame payload in which E is the escape byte, F is the flag byte, and D is a data byte other than an escape or a flag character. D DEF E D DD DE E
-
(a) How many different arrangements can be obtained from the letters of the word STATISTICS? [4 marks] (b) How many different arrangements can be obtained from the letters of the word SUMMER such...
-
1. Answer following 1 questions on the application of a design pattern. a. (5 points) You are designing a Car Racing video game such as Need for Speed or Forza Motor Sport. In your game, a user is...
-
3. Now assume you really get motivated to pay back the debt, and you want to have it paid off within 6 months. Find the sheet labeled "6 months." Find cell 12. It has a bold black box around it. You...
Study smarter with the SolutionInn App