我是一名新的Java程序员,我正在开发一个项目,要求我阅读包含电影评论的文本文件。通过Arraylist搜索并排序
一旦我阅读了文件,我被要求搜索和排序电影阵列,并返回每部电影的评论总数以及每部电影的平均评分。
我当前停留的部分正在迭代Array列表。
我正在使用一个内部和外部for循环,我似乎得到一个无限循环。
我会欣赏第二套眼睛。我一直在盯着这个项目几天,并开始不看错误。
下面是代码:
import java.io.*;
import java.util.*;
import java.lang.*;
public class MovieReviewApp {
public static void main(String[] args)
{
String strline = "";
String[] result = null;
final String delimit = "\\s+\\|\\s+";
String title ="";
//int rating = (Integer.valueOf(- 1));
ArrayList<MovieReview> movies = new ArrayList<MovieReview>();
//ArrayList<String> titles = new ArrayList<String>();
//ArrayList<Integer> ratings = new ArrayList<Integer>();
//HashMap<String, Integer> hm = new HashMap<String, Integer>();
//ListMultimap<String, Integer> hm = ArrayListMultimap.create();
try
{
BufferedReader f = new BufferedReader(new FileReader("/Users/deborahjaffe/Desktop/Java/midterm/movieReviewData.txt"));
while(true)
{
strline = f.readLine(); // reads line by line of text file
if(strline == null)
{
break;
}
result = strline.split(delimit, 2); //creates two strings
//hm.put(result[0], new Integer [] {Integer.valueOf(result[1])});
//hm.put(result[0], Integer.valueOf(result[1]));
// titles.add(result[0]);
//ratings.add(Integer.valueOf(result[1]));
MovieReview m = new MovieReview(result[0]);
movies.add(m);
MovieReview m2 = new MovieReview();
int rating = Integer.valueOf(result[1]);
int sz = movies.size();
for (int i = 0; i < sz; i++)
{
for (int j = 0; j < sz; j++)
{
m2 = movies.get(i);
if (movies.contains(m2))
{
m2.addRating(rating);
}
else
{
movies.add(m2);
m2.addRating(rating);
}
}
}
movies.toString();
//Collections.sort(movies);
} //end while
f.close();
//Set<String> keys = hm.keySet();
//Collection<Integer> values = hm.values();
} //end of try
catch(FileNotFoundException e)
{
System.out.println("Error: File not found");
}
catch(IOException e)
{
System.out.println("Error opening a file.");
}
} // end main
} // end class
我在打电话,所以代码很难阅读,但你有一段时间了。那是一个故意的无限循环吗? – ghostbust555 2015-04-01 19:20:48
它看起来不像一个无限循环,但你确实设置了一个指数循环结构。在1,000部电影中,您将添加一百万条评论,并且只会变得更糟。您可能想要尝试一部分电影(如5-10),以便您可以找到代码中的所有错误。 – JimW 2015-04-01 19:43:33
@DeborahJaffe我建议你快速查看一下我的答案,特别是关于使用扫描器而不是BufferedReader来获得更高效率和更多功能的部分。关于你在Sujan Reedy A的回答中提到的问题,你可以请扩展一下吗?我知道它没有被正确地读取,但是也许输入与代码的输出对于找出错误是更有用的。 – Ungeheuer 2015-04-03 17:40:28