Keep hitting same error message with this problem TXT File =====================
ID: 3790893 • Letter: K
Question
Keep hitting same error message with this problem
TXT File
=======================
7
Harry Potter and the Half-Blood Prince
4
Harry Potter and the Half-Blood Prince
5
Army of the Dead
1
Harry Potter and the Half-Blood Prince
4
Army of the Dead
2
The Uninvited
4
Pandorium
3
Exercise #4-Rating Problem (20pts) Consider a list of movie reviews you have compiled, where each movie receives a rating from 1 (bad) to 5 (excellent.) The first line in the file is the number of ratings that are in that particular file. After that each rating consists of two lines: e name of the movie the numeric rating between 1 to 5 Here is a sample file with four unique movies and seven ratings Harry Potter and the Half-Blood Prince Last Modified: 01/31/17 Harry Potter and the Half-Blood Prince Army of the Dead Harry Potter and the Half-Blood Prince Army of the Dead The Uninvited Pandorium For this exercise you are to write classes which will read in a file in the above format, calculate the average rating for each movie, and the output the average along with the number of reviews. Using the input file from above, sample output can be shown as follows Harry Potter and the Half-Blood Prince 3 reviews average of 4.3 5 Army of the Dead 2 reviews average of 1.5 5 The Uninvited 1 review average of 4 5 Pandorium: 1 review average of 3 5 Ensure to use an instance (or instances) of the class HashMap in your solution. Your map should index from a string representing each movie's name to integers that store the number of reviews for the movie, as well as the sum of ratings for the movie. Feel free to name the source code files anything to your liking when completing the solution.Explanation / Answer
ANSWER:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class RatingProblem {
public static HashMap<String, Integer> ratingScore = new HashMap<String, Integer>();
public static HashMap<String, Integer> numOfRatings= new HashMap<String, Integer>();
private static ArrayList<String> moviesList= new ArrayList<String>();
private static String totalRatings,movies;
private static int ratings;
private static String fileName = "resource/Rating.txt";
public static void main(String[] args) {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) {
totalRatings = bufferedReader.readLine();
for (int i = 0; i < Integer.parseInt(totalRatings); i++) {
movies = bufferedReader.readLine();
ratings = Integer.parseInt(bufferedReader.readLine());
if (!ratingScore.containsKey(movies)) {
ratingScore.put(movies, ratings);
numOfRatings.put(movies, 1);
moviesList.add(movies);
} else {
int totalTemp = (int) numOfRatings.get(movies);
int totalScore = (int) ratingScore.get(movies);
totalScore += ratings;
totalTemp++;
ratingScore.put(movies, totalScore);
numOfRatings.put(movies, totalTemp);
}
}
int size = moviesList.size();
int num[] = new int[size];
int totalAverage[] = new int[size];
int index =0;
for(Map.Entry<String, Integer> entry:numOfRatings.entrySet()) {
num[index] = entry.getValue();
index++;
}
index=0;
for(Map.Entry<String, Integer> entry:ratingScore.entrySet()) {
int total = entry.getValue();
totalAverage[index] = total/num[index];
index++;
}
for(int i=0; i<size;i++) {
System.out.println(i+1+" "+moviesList.get(i)+":" + num[i]+" of ratings, average of:"+totalAverage[i]+"/5");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Use ForEach loop for getting HashMap values.
And give your file path in the above code
Working fine. Let me know any concerns. Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.