2016-01-21 53 views
-1

我試圖讓學生記錄應用程序,我可以的姓名和性別從文本文件中分離出來,但是當我試圖把它們放入數組,數組總是顯示爲空。當我單獨嘗試時,addStudent方法起作用。如何將我的姓名和性別變量放入數組中?

 StudentList list = new StudentList(); 
     string name = ""; 
     string gender = ""; 
     string line = ""; 
     char[] selected = line.ToCharArray(); 

     openFileDialog1.ShowDialog(); 
     string file = openFileDialog1.FileName; 

     StreamReader sr = new StreamReader(file); 


     while ((line = sr.ReadLine()) != null) 
     { 
      studentList.Items.Add(line); 

      for (int i = 0; i < selected.Length; i++) 
      { 
       if (selected[i] != '(') 
       { 
        name += selected[i]; 

       } 

       else if (selected[i] == '(') 
       { 
        gender += selected[i + 1]; 
        break; 
       } 
       Student student = new Student(); 

       student.setName(name); 
       student.setGender(gender); 

       list.addStudent(student); 
      } 
     } 

這是其他類我爲了根據姓名和性別變量(不是想改變這種代碼)

class Student 
{ 
    private string name; 
    private string gender; 
    private DateTime birthDate; 

    public Student() 
    { 
     name = null; 
     gender = null; 
     birthDate = DateTime.Now; // default to set to current date 
    } 

    public void setName(string n) 
    { 
     name = n; 
    } 

    public string getName() 
    { 
     return name; 
    } 

    public void setGender(string g) 
    { 
     gender = g; 
    } 

    public string getGender() 
    { 
     return gender; 
    } 

    public void setBirthDate(DateTime bd) 
    { 
     birthDate = bd; 
    } 

    public DateTime getBirthDate() 
    { 
     return birthDate; 
    } 

} 

class StudentList 
{ 
    private Student[] list; 
    private int numStudents; 
    private int maxNum; 

    public StudentList() 
    { 
     maxNum = 300; 
     list = new Student[maxNum]; 
     numStudents = 0; 
    } 

    public Student getStudent(int index) 
    { 
     Student ret = null; 
     try 
     { 
      ret = list[index]; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Exception has occurred: " + ex.Message); 
     } 

     return ret; 
    } 

    public bool addStudent(Student s) 
    { 
     bool ret = false; 

     if (numStudents < maxNum-1) 
     { 
      list[numStudents] = s; 
      numStudents++; 

      ret = true; 
     } 
     else 
     { 
      MessageBox.Show("Maximum Students exceeded"); 

     } 

     return ret; 
    } 

    public int getNumStudents() 
    { 
     return numStudents; 
    } 

    public void clear() 
    { 
     for (int i = 0; i < numStudents; i++) 
      list[i] = null; // set to null 

     numStudents = 0; 

    } 

    public Student findStudent(string name) 
    { 
     Student ret = null; 

     for (int i = 0; i < numStudents; i++) 
     { 
      Student currentStudent = list[i]; 

      if (currentStudent.getName() == name) 
      { 
       ret = currentStudent; 
       break; 
      } 
     } 

     return ret; 
    } 

} 
+0

請提供一個輸入文件結構的示例。你可能可以用一個字符來分割一行,而不是循環通過一行字符。 – RokX

+0

這應該是一個windowsform應用程序,並且StudentList list = new StudentList();是創建一個已經有另一個類的數組。 - LamaTo – user3054895

+0

例如: 鮑勃(M) 瑪麗(F) 凱特瓊斯(F) -RokX – user3054895

回答

0

你需要把string name = "";string gender = "";selected = line.ToCharArray();while循環。您還應該在for循環外創建Student對象。

StudentList列表=新StudentList();

openFileDialog1.ShowDialog(); 
string file = openFileDialog1.FileName; 

StreamReader sr = new StreamReader(file); 

string line = ""; 
while ((line = sr.ReadLine()) != null) 
{ 

    string name = ""; 
    string gender = ""; 

    char[] selected = line.ToCharArray(); 
    for (int i = 0; i < selected.Length; i++) 
    { 
     if (selected[i] != '(') 
     { 
      name += selected[i]; 

     } 

     else if (selected[i] == '(') 
     { 
      gender += selected[i + 1]; 
      break; 
     } 

    } 

    Student student = new Student(); 

    student.setName(name); 
    student.setGender(gender); 

    list.addStudent(student); 
    //Add student objet rather than line string 
    studentList.Items.Add(student); 
} 

UPDATE 根據您的意見,我認爲你應該重寫ToString()方法爲您Studend class

public override string ToString() 
{ 
    return name + "("+ gender+")"; 
} 
+0

如果我移動在該串線不會工作 而((線= sr.ReadLine ())!= NULL) – user3054895

+0

對不起,我更新我的帖子 –

+0

它使觸發該通知我我的數組的限制我的消息框。限制爲300.懷疑是將每個單獨的字母添加到數組中。 – user3054895

0

像這樣的東西應該這樣做(我沒有編譯這麼小錯誤是可能的):

StudentList list = new StudentList(); 
    string line = ""; 

    openFileDialog1.ShowDialog(); 
    string file = openFileDialog1.FileName; 

    StreamReader sr = new StreamReader(file); 

    while ((line = sr.ReadLine()) != null) 
    { 
     string[] lineStudents = line.Split(')'); 

     foreach(string lineStudent in lineStudents) 
     { 
      Student student = new Student(); 
      string[] studentProps = lineStudent.Split('('); 
      student.setGender(studentProps[1]); 
      student.setName(studentProps[0].Trim()); 
      list.addStudent(student); 

     } 
    }