如何将我的姓名和性别变量放入数组?

时间:2016-01-21 12:41:10

标签: c#

我正在尝试创建学生记录应用程序,我能够将文本文件中的名称和性别分开,但是当我尝试将它们放入数组时,数组总是显示为空。当我单独尝试时,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;
    }

}

2 个答案:

答案 0 :(得分:0)

您需要将string name = "";string gender = "";selected = line.ToCharArray();放在while循环中。您还应该在Student循环之外创建for对象。

StudentList list = new 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);
}

<强>更新 根据您的评论,我认为您应该覆盖您的学生ToString()

class方法
public override string ToString()
{
   return name + "("+ gender+")";
}

答案 1 :(得分: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);

        }
    }