-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieMeans.java
More file actions
48 lines (47 loc) · 1.48 KB
/
MovieMeans.java
File metadata and controls
48 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
import java.io.*;
public class MovieMeans
{
public static Map<String, Double> makeRatingDictionary(String dir) {
Map<String, Double> ratings = new HashMap<String, Double>();
File dirName = new File(dir);
File[] dirFiles = dirName.listFiles();
AllMean mean = new AllMean(dir);
for (File f : dirFiles) {
try {
FileInputStream fin = new FileInputStream(f);
BufferedReader inFile = new BufferedReader(new InputStreamReader(fin));
String mvNum = inFile.readLine();
String nLine = inFile.readLine();
double tot = 0;
int num = 0;
while (nLine != null) {
String[] lineArray = nLine.split(",");
tot += Double.parseDouble(lineArray[1]) - mean.getMean();
num++;
nLine = inFile.readLine();
}
Double totRating = tot/num;
ratings.put(mvNum, totRating);
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ratings;
}
public static void main(String[] args) {
try {
FileWriter fout = new FileWriter("movieMeans.txt");
BufferedWriter outFile = new BufferedWriter(fout);
Map<String, Double> ratings = makeRatingDictionary(args[0]);
for (Map.Entry<String, Double> entry : ratings.entrySet()) {
outFile.write(entry.getKey() + entry.getValue());
outFile.newLine();
}
outFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}