2012-05-23 196 views
-3

我有一個任務要做,那就是製作電影檔案。這裏是我的任務:java方法錯誤

Besides the main program, create a custom class that stores information about a movie. This class is then used in the main program. The following information should be stored for each film

title 
length 
grade 
format 
year 

然後

In the main program, you should be able to store 1,000 films. The user should be able to do the following in the program (make a selection menu):

• Enter the information about a movie. Make a method for loading a movie from the user.

• Print the information on all films. Make a method for printing a film and use it.

• Save all your movies to a file. Please do your own method for this.

• Download movies from a file. Please do your own method for this.

的問題是,我與我的skrivUtInfo(PrintOut information)方法

我創建1000部電影的一個數組列表,得到了一個錯誤。但是我怎麼能打印1000部電影呢?

這裏是我的代碼:

public class Film { 
    String title ; 
    int  length; 
    int  grade ; 
    String format ; 
    int  year ; 
} 

import java.util.*; 
import java.io.*; 

public class Filmarkiv { 

    static Scanner sc = new Scanner(System.in); 

    public static void main(String[] arg) { 

     BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
     Scanner s = new Scanner(inFromUser); 

     Film[] film = new Film[1000]; 

     int antal = 1; 

     film[0] = new Film(); 
     film[0].title = "matrix"; 
     film[0].length = 220; 
     film[0].grade = 5; 
     film[0].format = "DVD"; 
     film[0].year = 1999; 

     while(true) 
     { 
      int n = 0; 
      System.out.println("valj 1 for inmata. 2 for utskrift"); 

      String val = s.next(); 

      if(val.equals("1")){ 
       System.out.println("vad heter filmen?"); 
       film[n].title = s.next(); 

       System.out.println("hur lang ar filmen?"); 
       film[n].length = s.nextInt(); 

       System.out.println("vad har den for betyg?"); 
       film[n].grade = s.nextInt(); 

       System.out.println("vad har den for format?"); 
       film[n].format = s.next() ; 

       System.out.println("i vilket år har filmen inspelat? "); 
       film[n].year = s.nextInt() ; 
      } 
      else if (val.equals("2")) 
      { 
       skrivUtInfo(film, antal); 

       /*System.out.println("title = "+film[n].title) ; 
       System.out.println("length = "+film[n].length) ; 
       System.out.println("grade = "+film[n].grade) ; 
       System.out.println("format = "+film[n].format) ; 
       System.out.println("year = "+film[n].year);*/ 
      } 
     } 
    } 

    public skrivUtInfo (Film[] qwert, int a) { 
     for (int n=0; n<a; n++) { 
      System.out.println("title = "+film[n].title) ; 
      System.out.println("length = "+film[n].length) ; 
      System.out.println("grade = "+film[n].grade) ; 
      System.out.println("format = "+film[n].format) ; 
      System.out.println("year = "+film[n].year) ; 

      return Film[]; 
     } 
    } 
} 
+1

什麼是錯誤?什麼是堆棧跟蹤? –

+0

非法開始表達 – Semthan

回答

1

如果您已經瞭解java.util.List,你可以這樣來做:

List<Film> films = new ArrayList<Film>(); 
films.add(new Film("Amadeus", 120, 5, "DVD", 1984); ); // Add as many as you like 
System.out.println(films); // Make sure your Film class overrides toString() 

如果你還沒有,只是做在一個循環:

Film [] films = new Film[1000]; 
films[0] = new Film("Amadeus", 120, 5, "DVD", 1984); 
for (Film film : films) { 
    System.out.println(film); // Make sure you File class overrides toString() 

} 

你可能有問題,因爲你的電影類有瑕疵。這樣做:

public class Film { 

    private final String title ; 
    private final int  length; 
    private final int  grade ; 
    private final String format ; 
    private final int  year ; 

    public Film(String title, int length, int grade, String format, int year) { 
     this.title = title; 
     this.length = length; 
     this.grade = grade; 
     this.format = format; 
     this.year = year; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public int getLength() { 
     return length; 
    } 

    public int getGrade() { 
     return grade; 
    } 

    public String getFormat() { 
     return format; 
    } 

    public int getYear() { 
     return year; 
    } 

    @Override 
    public String toString() { 
     final StringBuilder sb = new StringBuilder(); 
     sb.append("Film"); 
     sb.append("{title='").append(title).append('\''); 
     sb.append(", length=").append(length); 
     sb.append(", grade=").append(grade); 
     sb.append(", format='").append(format).append('\''); 
     sb.append(", year=").append(year); 
     sb.append('}'); 
     return sb.toString(); 
    } 
} 
+0

比X 我做到了,但我得到空?? – Semthan

+0

做了什麼?發佈一些代碼。你做錯了。 – duffymo

0

方法public skrivUtInfo (Film[] qwert, int a)沒有指定返回類型。在Java中,它應該有void或有效類型作爲返回,但我看到你返回Film[]。你不能那樣做。你必須聲明像

public Film[] skrivUtInfo (Film[] qwert, int a){ 
    .... 
    return qwert; 
} 

最終,因爲你只是在這裏打印的東西,你不需要返回任何東西。在這種情況下,你應該有

public void skrivUtInfo (Film[] qwert, int a){ 
    ..... 
    return; 
} 

你可以有一個空的回報或沒有在具有void返回類型的方法指定的回報。

1

你必須改變方法

public static void skrivUtInfo (Film[] qwert) { 
    for (int n=0; n<qwert.length; n++) { 
     System.out.println("title = "+qwert[n].title) ; 
     System.out.println("length = "+qwert[n].length) ; 
     System.out.println("grade = "+qwert[n].grade) ; 
     System.out.println("format = "+qwert[n].format) ; 
     System.out.println("year = "+qwert[n].year) ; 
    } 
} 

也把這裏的支架

else if (val.equals("2")) 
{ 
    skrivUtInfo(film); 
    // the comments 
} 
} //<- you must add this bracket. is from while (i think) 

也提示,在電影類,你可以從Object類重寫toString方法

public class Film{ 
    String title ; 
    int  length; 
    int  grade ; 
    String format ; 
    int  year ; 

    public String toString() { 
    return "title = " + title + 
      "length = " + length + 
      "grade = " + grade + 
      "format = " + format + 
      "year = " + year; 

    } 
} 

so skrivUtInfo變成

public static void skrivUtInfo (Film[] qwert) { 
    for (int n=0; n<qwert.length; n++) { 
     System.out.println(qwert[n]); 
    } 
} 

public static void skrivUtInfo (Film[] qwert) { 
    for (Film f : qwert) { 
     System.out.println(f); 
    } 
} 
+0

謝謝你的人尋求幫助,但我得到了一些錯誤 Filmarkiv.java:75:錯誤:錯誤:表達 \t公共無效skrivUtInfo(電影[] QWERT){ \t^ Filmarkiv.java:75的非法啓動:非法開始表達 \t public void skrivUtInfo(Film [] qwert){ \t^ Filmarkiv.java:75:error:';'預計 \t public void skrivUtInfo(Film [] qwert){ \t^ Filmarkiv.java:75:error:';'預期 \t公共無效skrivUtInfo(電影[] QWERT){ \t^ 4個錯誤 工具退出碼1 – Semthan

+0

我的錯完成。將skrivUtInfo聲明爲靜態。你需要關閉支架 – pbaris

+0

okey,謝謝你 – Semthan

0

我有測試此代碼,並能正常工作。我改變了幾個部分,因爲我認爲它應該發揮

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.util.Scanner; 

public class Filmarkiv { 
    static Scanner sc = new Scanner(System.in); 

    public static void main(String[] arg) { 
     BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
     Scanner s = new Scanner(inFromUser); 

     Film[] film = new Film[1000]; 
     int n = 0; 

     do { 
      System.out.println("valj 1 for inmata. 2 for utskrift"); 
      String val = s.next(); 

      if (val.equals("1")) { 
       film[n] = new Film(); 

       System.out.println("vad heter filmen?"); 
       film[n].title = s.next(); 

       System.out.println("hur lang ar filmen?"); 
       film[n].length = s.nextInt(); 

       System.out.println("vad har den for betyg?"); 
       film[n].grade = s.nextInt(); 

       System.out.println("vad har den for format?"); 
       film[n].format = s.next(); 

       System.out.println("i vilket år har filmen inspelat? "); 
       film[n].year = s.nextInt(); 

       n++; 
      } else if (val.equals("2")) { 
       skrivUtInfo(film); 
      } 
     } while(n < film.length); 

    } 

    public static void skrivUtInfo (Film[] qwert) { 
     for (Film f : qwert) { 
      if (f != null) 
       System.out.println(f); 
     } 
    } 
} 

class Film { 
    String title; 
    int length; 
    int grade; 
    String format; 
    int year; 

    public String toString() { 
     return "title = " + title + ", length = " + length + ", grade = " + grade 
       + ", format = " + format + ", year = " + year; 

    } 
} 
+0

謝謝你,我會嘗試它:) – Semthan