2012-01-29 13 views
20

Nie mogę uruchomić poniższego kodu w środowisku Eclipse. Mam główną metodę i jest to aktualnie otwarty plik. Próbowałem nawet opcji "Uruchom jako", ale ciągle dostaję ten błąd: "edytor nie zawiera głównego typu". Co ja tu robię źle?Błąd Eclipse: "Edytor nie zawiera głównego typu"

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

Jaka jest nazwa pliku zawierającego ten kod? – Zyerah

+0

cfiltering.java – JJJ

+1

Myślę, że jest to całkowicie poprawne pytanie. W rzeczywistości wpadłem na to i zrozumiałem mój problem: utworzyłem klasę (omyłkowo) pod folderem źródłowym: src/main/test, a eclipse nie mógł sobie z tym poradzić. Kiedy poprawnie utworzyłem go w src/test/java, ten błąd zniknął. – Jack

Odpowiedz

20

Spróbuj zamknąć i ponownie otworzyć plik, a następnie naciśnij przycisk Ctrl+F11.

Sprawdź, czy nazwa pliku, którego używasz, jest taka sama, jak nazwa projektu, nad którym pracujesz, oraz że nazwa klasy publicznej w tym pliku jest taka sama jak nazwa projektu, którym jesteś również w pracy.

W przeciwnym razie uruchom ponownie Eclipse. Daj mi znać, jeśli to rozwiąże problem! W przeciwnym razie komentarz, a ja postaram się pomóc.

+0

Teraz, gdy uruchomiłem go we własnym kodzie, nie dostaję błędów. Spróbuj odtworzyć projekt? – Zyerah

+1

Wydawało się, że to się udało, dzięki! – JJJ

+2

Restartowanie pomogło mi, jeśli ma jakieś interesy. – kiltek

3

Czy importować pakiety rzeczy do czytania plików.

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

również tutaj

cfiltering(numberOfUsers, numberOfMovies); 

Czy starasz się stworzyć obiekt lub wywołanie metody?

też inna rzecz:

user_movie_matrix[userNo][movieNo]=rating; 

jesteś przypisanie wartości do członka instancji, jak gdyby to była statyczna zmienna również usunąć Th w

private int user_movie_matrix[][];Th 

nadzieję, że to pomaga.

+0

Próbuję wywołać metodę, która zmienia wymiary „user_movie_matrix” i „user_user_matrix” – JJJ

+0

oh widzę ... więc co u należy zrobić, to – bernabas

+0

oh widzę ... więc co u należy zrobić, to utworzyć klasę metoda podobna do'set_user_movie_matrix (x, y) {this.userNo = x; this.movieNo = y;} "', aby zmienić te wartości obiektu ...następnie zamiast tworzyć obiekt bez nazwy, wykonaj "cfiltering nazwa_zmiennej = nowe cfilterowanie (numberOfUsers, numberOfMovies);" , a następnie używając tej zmiennej, wywołaj metody takie jak 'nazwa_zmiennej.set_user_movie_matrix (userNo, movieNo) ' – bernabas

1

private int user_movie_matrix[][];Th. powinien być `private int user_movie_matrix[][];.

private int user_movie_matrix[][]; powinny być private static int user_movie_matrix[][];

cfiltering(numberOfUsers, numberOfMovies); powinny być new cfiltering(numberOfUsers, numberOfMovies);

, czy kod działa zgodnie z przeznaczeniem po tych zmian jest poza zakresem tej odpowiedzi; było kilka błędów składni/zakresu.

+0

Nie zamierzałem używać user_movie_matrix być zmienną statyczną. Czy musi być statyczny, aby działał prawidłowo? – JJJ

+0

Musi być statyczny, ponieważ kod jest zapisany, ponieważ ** bezpośrednio ** uzyskuje dostęp z metody statycznej, a mianowicie 'public static void main (String [] args)'. –

Powiązane problemy