2012-01-29 3 views
20

Eclipse에서 다음 코드를 실행할 수 없습니다. 나는 main 메소드를 가지고 있으며, 이것은 현재 열린 파일이다. 나는 "Run As"옵션을 시도해 보았지만 "Editor는 메인 타입을 가지고 있지 않다."라는 에러가 계속 발생했다. 여기서 내가 뭘 잘못하고 있니?Eclipse 오류 : "편집기에 기본 유형이 없습니다."

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()); 
     } 
    } 

} 
+3

이 코드가 포함 된 파일의 이름은 무엇입니까? – Zyerah

+0

cfiltering.java – JJJ

+1

저는 이것이 아주 유효한 질문이라고 생각합니다. 사실, 나는 이것에 부딪쳤다 내 문제를 알아 냈어 : 나는 (실수로) 원본 폴더 아래에 클래스를 만들었 : src/main/test, 그리고 일식 그것을 처리 할 수 ​​없었다. 내가 src/test/java에서 정확하게 만들었을 때,이 오류는 사라졌다. – Jack

답변

20

파일을 닫았다가 다시 열어 본 다음 Ctrl+F11을 누르십시오.

실행중인 파일의 이름이 현재 작업중인 프로젝트의 이름과 같고 해당 파일의 공용 클래스 이름이 현재 사용중인 프로젝트의 이름과 같은지 확인하십시오 뿐만 아니라에서 일하고.

그렇지 않으면 Eclipse를 다시 시작하십시오. 이것이 문제를 해결하면 알려주세요! 그렇지 않으면 의견을 말하면 도움을 드리겠습니다.

+0

이제 코드를 내 코드로 실행 했으므로 오류가 발생하지 않습니다. 프로젝트를 재생성 해보십시오. – Zyerah

+1

그 트릭을 한 것처럼 보였다, 고마워! – JJJ

+2

재시작은 누군가 관심사가 있다면 도움이되었습니다. – kiltek

3

파일을 읽는 데 필요한 패키지를 가져 왔습니까?

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 

도 여기에

cfiltering(numberOfUsers, numberOfMovies); 

당신은 개체를 만들려고 또는 메소드를 호출하고 있습니까?

또 다른 일이 : 그것은 정적 변수 또한이 도움이

private int user_movie_matrix[][];Th 

희망의 Th를 제거 것처럼

user_movie_matrix[userNo][movieNo]=rating; 

하면 인스턴스의 멤버에 값을 할당한다.

+0

"user_movie_matrix"및 "user_user_matrix"의 크기를 변경하는 메서드를 호출하려고합니다. – JJJ

+0

oh ... 그래서 무엇을해야합니까? – bernabas

+0

오, 알겠습니다 ... 그래서 무엇을해야할까요? 클래스를 만드는 것입니다. like 'set_user_movie_matrix (x, y) {this.userNo = x; this.movieNo = y;} ''객체의 값을 변경하려면 ...'variable_name = new cfiltering (numberOfUsers, numberOfMovies);' 을 호출 한 다음 해당 변수를 사용하여 'variable_name.set_user_movie_matrix (userNo, movieNo)'와 같은 메소드를 호출하십시오. – bernabas

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);

되는지 여부해야해야 코드 변경 후 의도 한대로이 답변의 범위를 벗어 작동; 구문/범위 지정 오류가 여러 개 있습니다.

+0

user_movie_matrix를 사용하지 않았습니다. 정적 변수가됩니다. 제대로 작동하려면 정적이어야합니까? – JJJ

+0

정적 메서드, 즉'public static void main (String [] args)'에서 ** 직접 ** 접근하기 때문에 코드가 작성 될 때 정적이어야합니다. –

관련 문제