有个电影评分的INPUT 和output的问题 有没有大神

qq_31788007 2015-10-12 06:02:11
先给个TXT文件:


Casablanca
8.7
10.0
Toy Story
8.1
10.0
Star Wars
88.0
100.0
Toy Story
4.0
5.0
文件每三行是一个块(电影名字,评分,总分),目标是要把所有的总分都变为10.0,就是说把(88.0/100)变成(8.8/10)把(4.0/5.0)变成(8.0/10.0),然后把名字一样的电影分数合成一个电影名字,然后他们的分数变成平均分(比如这有两个 Toy story,结果只print出一个,分数变成8.05)

我现在有的这个程序只能反复读出一行字母一行数字,就是说只能读出下面这个每两行是一个块(电影名字,评分)的文件(程序在下面给出),
Casablanca
8.7
Toy Story
8.1
Star Wars
88.0
Toy Story
4.0




现在平均分还有合并名字的问题下面程序已经写好了,问题出在try-catch里面,请问大神要怎么改try-catch里的东西

import java.io.*;

public class Activity2D {
public static void main(String[] args) {
BufferedReader input;
String title, ratingText;
double rating = 0.0;

Review[] movies = new Review[100];
int size = 0;
Review match;

//从这里开始改

try {
input = new BufferedReader(new FileReader("movies.txt"));

title = input.readLine();
while (title != null) {
ratingText = input.readLine();

try {

rating = Double.parseDouble(ratingText);


// if the conversion failed and the code below was not in the
// "try" block, the rating variable would contain the rating of
// the <em>previous</em> movie we read in (bad data)
match = findReview(movies, size, title);
if (match == null) {
// movie that was not previously listed
movies[size] = new Review(title, rating);
size++;
} else {
// this movie was already reviewed at least once
match.addRating(rating);
}

} catch (NumberFormatException nfe) {
System.out.println("Invalid rating: " + ratingText);
}

title = input.readLine();
}

input.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}

for (int i = 0; i < size; i++) {
System.out.println(movies[i]);
}

System.out.println("\nFinished processing.");
}
//到这里结束

public static Review findReview(Review[] movies, int size, String title) {
Review result = null;
int pos;

pos = 0;
while (pos < size && result == null) {
if (movies[pos].matchTitle(title)) {
result = movies[pos];
} else {
pos++;
}
}

return result;
}
}

class Review {
private String title;
private double totalRating;
private int reviewCount;

public Review(String title, double rating) {
this.title = title;
totalRating = rating;
reviewCount = 1;
}

public void addRating(double rating) {
totalRating += rating;
reviewCount++;
}

public boolean matchTitle(String otherTitle) {
// returns true if this movie's title matches otherTitle
return title.equals(otherTitle);
}

public String toString() {
return title + " (rating: " + (totalRating / reviewCount) + ")";
}
}


...全文
140 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
BufferedReader fr = new BufferedReader(new FileReader(new File(
"F:\\JavaSE\\movie.txt")));里的路径改为自己的路径
  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Set;

public class MovieScore {
private static LinkedHashMap<String, ArrayList<double[]>> map = new LinkedHashMap<>();
private static LinkedHashMap<String, Double> map1 = new LinkedHashMap<>();

public static void main(String[] args) {
try {
BufferedReader fr = new BufferedReader(new FileReader(new File(
"F:\\JavaSE\\movie.txt")));
String name;
while ((name = fr.readLine()) != null) {
double first = Double.parseDouble(fr.readLine());
double second = Double.parseDouble(fr.readLine());
if (map.containsKey(name)) {
map.get(name).add(new double[] { first, second });
} else {
ArrayList<double[]> list = new ArrayList<>();
list.add(new double[] { first, second });
map.put(name, list);
}
}
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
parse();
print(map1);
}

public static void parse(){
Set<String> set = map.keySet();
for (String string : set) {
ArrayList<double[]> list = map.get(string);
if(list.size() > 1){
double d = 0;
for (double[] ds : list) {
d += review(ds);
}
map1.put(string, d/list.size());

}else{
map1.put(string, review(list.get(0)));
}
}

}
public static double review(double[] d){
if(d[1] == 100){
d[0] /= 10;
}
if(d[1] == 5){
d[0] *= 2;
}
return d[0];
}
public static void print(LinkedHashMap map){
for (Object obj : map.keySet()) {
System.out.println(obj);
System.out.println(map.get(obj));
}
}
}
qq_31788007 2015-10-12
  • 打赏
  • 举报
回复
不是特别明白,最好能写出来 让我看看明白- -
  • 打赏
  • 举报
回复
你可以读一行,判断是不是字符串,是的话再读两行,用map存,字符串为key,value数组存小数,读完后分析
qq_31788007 2015-10-12
  • 打赏
  • 举报
回复
现在我读出不总分,一加上每第三行的总分就会出来错的排序,怎么改才能让它正确显示,如下

Casablanca (rating: 8.7)
Toy Story (rating: 8.05)
Star Wars (rating: 8.8)

Finished processing.

50,452

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧