有个电影评分的INPUT 和output的问题 有没有大神
先给个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) + ")";
}
}