简体   繁体   中英

read file and store values into a 2D array

I have a file that consists of:

S5555; 100 70 70 100
S3333; 50 50 50 50
S2222; 20 50 40 70
S1111; 90 80 90 85
S4444; 70 80 90 50

When the user clicks button 1, it should load the file store the student ID into studentIDArr (eg S5555) and the other values into the 4x5 array marksMatrix, each value taking one position in the array.

Am I storing the values into studentIDArr correctly? As for the marksMatrix I tried to roughly code out how I think it works but I'm not totally sure as well (some commented out). I can only use arrays for this.

string[,] marksMatrix = new string[4,5];
string[] studentIDArr = new string[5];

private void button1_Click(object sender, EventArgs e)
{
    textBox2.Clear();

    try
    {
        using (StreamReader sr = new StreamReader("C:/Users/Y400/dDesktop/CTPrac/CTPrac/input.txt"))
        {
            string x = null;
            while ((x = sr.ReadLine()) != null)
            {
                for (int j = 0; j < studentIDArr.Length; j++)
                {
                    studentIDArr[j] = x;
                }
            }
        }

        textBox2.Text = "File Loading done.\r\n";
        textBox2.Text += "Number of records read: " + studentIDArr.Length;
    }
    catch (IOException ex)
    {
        textBox2.Text = "The file could not be read. " + ex.Message;
    }

    string a, b, c;
    for (int i = 0; i < studentIDArr.Length; i++)
    {
        //a = (String)studentIDArr[i];

        //    string[] abc = Regex.Split(a, ";");
        //    b = abc[0];
        //    c = abc[1];
        //    bc =; 

        for (int y = 0; y < 6; y++)
        {
            for (int x = 0; x < 5; x++)
            {
                //marksMatrix[y, x] = z;
            }
        }
    }

    button1.Enabled = false;
}

You can do that simply with for loops.

string[,] marksMatrix = new string[4, 5];
string[] studentIDArr = new string[5];

var lines = File.ReadAllLines("C:/Users/Y400/dDesktop/CTPrac/CTPrac/input.txt");
for (int i = 0; i < lines.Length; i++)
{
    var parts = lines[i].Split(new[] {';', ' ' });
    studentIDArr[i] = parts[0];
    for (int j = 1; j < parts.Length; j++)
    {
        marksMatrix[j - 1, i] = parts[j];
    }
}

this way of coding is so unreadable, try defining a class to store the student Id and the marks.

 public class Student
 {
     public string Id { get; set; }
     public List<string> Marks { get; set; }

     public Student()
     {
         this.Marks = new List<string>();
     }
 }

and then the code will looks like this

var students = new List<Student>();
foreach (var line in File.ReadLines("C:/Users/Y400/dDesktop/CTPrac/CTPrac/input.txt"))
{
    var parts = line.Split(new[]{';', ' '}).ToList();
    students.Add(new Student()
    {
        Id = parts[0],
        Marks = parts.GetRange(1, 4)
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM