2014-12-05 117 views
-1

StudentDemo:調用的方法在另一類來創建一個數組

public class studentDemo { 
    public static void main(String[] args) throws IOException{ 
     int runit = 0; 

     StudentMenu run = new StudentMenu(); 
     while (runit != 1) { 
      run.menu(); 
     } 
    } 
} 

StudentMenu:

public class StudentMenu { 

    public Student func = new Student(); 
    List<Student> studentList = new ArrayList<Student>(); 

    String input; 
    String input2; 
    String input3; 

    public void menu(){ 
     System.out.println("Choose an option: "); 
     System.out.println("1) Add a student "); 
     System.out.println("2) Display student list "); 
     System.out.println("3) Exit "); 

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     try { 

      input = br.readLine(); 
      int choice = Integer.parseInt(input); 
      System.out.println("Choice: " + choice); 

      if(choice == 1){ 
       System.out.println("First name: "); 
       input = br.readLine(); 
       System.out.println("Last name: "); 
       input2 = br.readLine(); 
       System.out.println("Number of classes to add for student: "); 
       input3 = br.readLine(); 
       int numberOfClasses = Integer.parseInt(input3); 

       studentList.add(new Student(input, input2, numberOfClasses)); 

       func.createClassList(numberOfClasses); 
      } 

      if(choice == 2){ 
      } 

      if(choice == 3){ 
       System.exit(0); 
      }  
     } 
     catch (IOException ioe){ 
      System.out.println("An unexpected error occured."); 
     } 
    } 
} 

學生:

public class Student{ 

    private String firstName; 
    private String lastName; 
    private int classes; 
    //Create array for classes 
    private String[] classesList = new String[classes]; 

    public Student(){ 
     firstName = "no name"; 
     lastName = "no name"; 
     classes = 0; 
    } 

    //overloaded constructor 
    public Student(String theFirstName, String theLastName, int theClasses){ 

     firstName = theFirstName; 
     lastName = theLastName; 
     classes = theClasses; 
     System.out.println("Number of Classes: " + classes); 
    } 

    //Populating array 
    public void createClassList(int classes){ 

     //Call array method 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     try{  
      for(int i = 0; i < classes; i++){ 
       System.out.println("Number of Classes: " + classes); 
       System.out.println("Enter class " + i + ":"); 
       String input = br.readLine(); 
       classesList[i] = input; 
      } 
     } 
     catch (IOException ioe) { 
      System.out.println("An unexpected error occured."); 
     } 
    } 

    //Getters 
    public String getFirstName(){ return firstName; } 

    public String getLasName(){ return lastName; } 

    public int getNumberOfClasses(){ return classes; } 

    //Setters 
    public void setFirstName(String newFirstName){ 
     firstName = newFirstName; 
    } 

    public void setLastName(String newLastName){ 
     firstName = newLastName; 
    } 

    public void setNumberOfClasses(int newNumberOfClasses){ 
     classes = newNumberOfClasses; 
    } 
} 

很抱歉,如果我過去有點不正確地編輯了這一點。

我想創建一個程序,用他們的名字,姓氏和類別列表建立一個學生列表。不過,我一直運行到這個錯誤輸入第一個下課:

,這裏是錯誤之前的輸出:

右後
Choose an option: 
1) Add a student 
2) Display student list 
3) Exit 
1 
Choice: 1 
First name: 
t 
Last name: 
s 
Number of classes to add for student: 
3 
Enter class 0: 
eng 

打字ENG,錯誤出現。
不應該有我可以告訴的OutOfBoundsException,因爲3是索引的限制,我只能在一個類中輸入。

任何幫助將不勝感激,謝謝!

+0

你能否嘗試將代碼粘貼是否正確?它會幫助我們幫助你。 – 2014-12-05 01:43:35

回答

0
private String[] classesList = null; 

//overloaded constructor 
public Student(String theFirstName, String theLastName, int theClasses) 
{ 
    firstName = theFirstName; 
    lastName = theLastName; 
    classes = theClasses; 

    classesList = new String[classes]; // THIS 

    System.out.println("Number of Classes: " + classes); 
} 

試試呢?

另一個問題與您的代碼:

public Student func = new Student(); 
List<Student> studentList = new ArrayList<Student>(); 

studentList.add(new Student(input, input2, numberOfClasses)); 
func.createClassList(numberOfClasses); 

正如你應該注意到,FUNC是與默認構造函數創建
您的Student類中的類變量將不會使用任何適當的值進行初始化,並且您的Student類中的String []不會被正確創建!
您在FUNC這是使用默認構造函數創建調用createClassList,而不是與numberOfClasses從用戶的輸入值。

Read what I have said, WaterlessStraw! :)

什麼,你可能想要做的,而不是:

public Student func = null; 
List<Student> studentList = new ArrayList<Student>(); 

func = new Student(input, input2, numberOfClasses); 
studentList.add(func); 
func.createClassList(numberOfClasses); 
2

此:

private int classes; // default value of 0 

private String[] classesList = new String[classes]; 

創建一個長度爲0的字符串數組,這將沒有多大用處給你的。將classesList數組聲明爲一個字段,但在之後僅爲其分配一個新數組,該數組設置了具有可行的非0 int值的類。

+0

謝謝你回到我身邊。我試圖在構造函數之後創建classesList。我想這會給類變量一個值,不是嗎?它似乎仍然沒有工作。 – WaterlessStraw 2014-12-05 01:49:51

+0

@WaterlessStraw:請告訴更多。「它似乎還沒有工作」告訴我們沒什麼用處。 – 2014-12-05 01:53:58

+0

我得到了和以前一樣的錯誤。 – WaterlessStraw 2014-12-05 02:05:48

相關問題