我似乎无法在Eclipse中运行以下代码。我有一个main方法,这是当前打开的文件。我甚至尝试了“运行方式”选项,但我不断收到此错误:“编辑器不包含主类型”。我在这里做错了什么?
public class cfiltering {
/**
* @param args
*/
//remember this is just a reference
//this is a 2d matrix i.e. user*movie
private static int user_movie_matrix[][];
//remember this is just a reference
//this is a 2d matrix i.e. user*user and contains
//the similarity score for every pair of users.
private float user_user_matrix[][];
public cfiltering()
{
//this is default constructor, which just creates the following:
//ofcourse you need to overload the constructor so that it takes in the dimensions
//this is 2d matrix of size 1*1
user_movie_matrix=new int[1][1];
//this is 2d matrix of size 1*1
user_user_matrix=new float[1][1];
}
public cfiltering(int height, int width)
{
user_movie_matrix=new int[height][width];
user_user_matrix=new float[height][height];
}
public static void main(String[] args) {
//1.0 this is where you open/read file
//2.0 read dimensions of number of users and number of movies
//3.0 create a 2d matrix i.e. user_movie_matrix with the above dimensions.
//4.0 you are welcome to overload constructors i.e. create new ones.
//5.0 create a function called calculate_similarity_score
//you are free to define the signature of the function
//The above function calculates similarity score for every pair of users
//6.0 create a new function that prints out the contents of user_user_matrix
try
{
//fileinputstream just reads in raw bytes.
FileInputStream fstream = new FileInputStream("inputfile.txt");
//because fstream is just bytes, and what we really need is characters, we need
//to convert the bytes into characters. This is done by InputStreamReader.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
int numberOfUsers=Integer.parseInt(br.readLine());
int numberOfMovies=Integer.parseInt(br.readLine());
//Now you have numberOfUsers and numberOfMovies to create your first object.
//this object will initialize the user_movie_matrix and user_user_matrix.
new cfiltering(numberOfUsers, numberOfMovies);
//this is a blankline being read
br.readLine();
String row;
int userNo = 0;
while ((row = br.readLine()) != null)
{
//now lets read the matrix from the file
String allRatings[]=row.split(" ");
int movieNo = 0;
for (String singleRating:allRatings)
{
int rating=Integer.parseInt(singleRating);
//now you can start populating your user_movie_matrix
System.out.println(rating);
user_movie_matrix[userNo][movieNo]=rating;
++ movieNo;
}
++ userNo;
}
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
}
}
答案 0 :(得分:21)
尝试关闭并重新打开该文件,然后按Ctrl+F11
。
验证您运行的文件的名称是否与您正在使用的项目的名称相同,并且该文件中的公共类的名称与您正在使用的项目的名称相同同样。
否则,重启Eclipse。如果这解决了问题,请告诉我!否则,请发表评论,我会尽力帮助。
答案 1 :(得分:3)
您是否为文件读取内容导入了包。
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
也在这里
cfiltering(numberOfUsers, numberOfMovies);
您是在尝试创建对象还是调用方法?
另一件事:
user_movie_matrix[userNo][movieNo]=rating;
您正在为实例的成员分配值,就好像它是一个静态变量一样 同时删除
中的Th
private int user_movie_matrix[][];Th
希望这有帮助。
答案 2 :(得分:1)
private int user_movie_matrix[][];Th
。应该是`private int user_movie_matrix[][];
。
private int user_movie_matrix[][];
应为private static int user_movie_matrix[][];
cfiltering(numberOfUsers, numberOfMovies);
应为new cfiltering(numberOfUsers, numberOfMovies);
在这些更改之后代码是否按预期工作超出了本答案的范围;有几个语法/范围错误。