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: 50% (4 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...
-
The number of people responding to a mailed information brochure on cruises of the Royal Viking Line through an agency in San Francisco is approximately normally distributed. The agency found that...
-
From your analysis, does an organization in your community consistently exceed state, national, and local performance metrics? If so, would you recommend this organization to your family and friends?
-
Allocating resources can be a political and an ad hoc activity in firms that do not use strategic management. Why is this true? Does adopting strategic management ensure easy resource allocation? Why?
-
Question 23 --/1 View Policies Current Attempt in Progress An "Ordering and Receiving Materials" cost pool would most likely have as a cost driver: O machine hours. O number of purchase orders. O...
-
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)
-
Why is the energy of the 3s state considerably lower than that of the 3p state for sodium, whereas in hydrogen these states have essentially the same energy?
-
Tristan Walker of Walker & Company says, "We are only going to design, develop, and test products and services uniquely tailored to our community's needs. I get it. I'm a part of the community we are...
-
Ace Cosmetics Corporation purchased land adjacent to its plant to improve access for trucks making deliveries. Expenditures incurred in purchasing the land were as follows: purchase price, $55,000;...
-
7. At this point you now know information about both the horizontal and the vertical components of the projectile's velocity. In the space below, draw a diagram of the vector components of Vx and...
-
Complete autonomy in how you demonstrate the following criteria. In this module, we talked more about leadership. We discussed the differences between leadership theory which is a well-substantiated...
-
Accustart Ltd. acquired 38% of the common shares of Lecce Ltd. on January 1, 2024, by paying $5.76 million for 144,000 shares. Lecce declared a cash dividend of $0.60 per share in each quarter that...
-
For each pair of function in Exercise f(x) = 2x + 3, g(x) = 3 - 5x (a) Find the domain of f, g, f + g, f - g, fg, f f, f/g, and g/f. (b) Find (f + g) (x), (f- g) (x), (fg) (x), (ff) (x), (f/g) (x),...
-
In Exercises 1558, find each product. (9 - 5x) 2
-
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
-
If the auditor believes that the financial statements prepared on the basis of the entity's income tax are not adequately titled, the auditor should : A)Issue a resignation of opinion. B)Explain the...
-
initial stock offering to the public. This REIT specializes in the acquisition and management of warehouses. Your firm, Blue Street Advisors, is an investment management company that is considering...
-
Question 3 You have been hired to run a pension fund for Mackay Inc, a small manufacturing firm. The firm currently has Gh5 million in the fund and expects to have cash inflows of $2 million a year...
Study smarter with the SolutionInn App