Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

File CountAlbums.java contains an incomplete program. The goal of the program is

ID: 3776324 • Letter: F

Question

File CountAlbums.java contains an incomplete program. The goal of the program is to process a file listing albums produced by various bands, and print out the number of albums for each band, in descending order of the number of albums.

An example file that your program should be able to process is albums.txt, which contains a list of albums that are in Rolling Stone Magazine's list of top 100 albums of all time. Each line is formatted as follows:

Note that the name of the band and the name of the album may themselves contain spaces, commas, or periods. However, you can assume that the name of the band does NOT contain the hyphen character '-'. You can also assume that there will be exactly one space character between the band name and the hyphen character. Examples of such lines are these:

Your task is to complete the CountAlbums.java program, by writing a function void processFile(String filename) that:

Takes the name of the input file as an argument.

Reads the file and counts, for each band, how many albums of that band are listed in the file.

Prints on the screen, in descending order of the number of albums, a line for each band. Each line should contain the name of the band, followed by a space, a colon, a space, and then the number of albums for that band. This would look like this:

Note that your program should work with ANY file that satisfies the format described above, and not just with the provided example file, albums.txt.

IMPORTANT: You are NOT allowed to modify in any way the main function. You are free to define and use auxiliary functions. You are also free to use code written in class, posted on the course website, or available on the lecture slides.

Also, your program should not crash under any circumstances. If the file does not exist, or the file does not follow the specified format, your program should handle such cases. You can handle such cases any way you like, as long as your program does not crash.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

example text file to use

http://vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/assignments/assignment10/albums.txt

Explanation / Answer

private static void processFile(String input_name) { ArrayList albumList = readFile(input_name); //get map Map bandCountMap = albumList.stream() .map(album -> album.split("-")[0].trim()) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); //sort by values Map sortedBandCountMap = bandCountMap.entrySet() .stream() .sorted(Map.Entry.comparingByValue(Collections.reverseOrder())) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new )); //lets print map Iterator it = sortedBandCountMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); System.out.println(pair.getKey() + " : " + pair.getValue()); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote