Home > Back-end >  Outputting names and test scores from a CSV File C#
Outputting names and test scores from a CSV File C#

Time:12-04

I have a project which I have created that reads from a CSV File which contains names and scores of students in a class. The program is supposed to read from the CSV File and then output all of the names. I have created the program and it works, it's just that I can only output the name and scores of 1 student in the class when it is meant to be for 12. If anyone could show me where I need to change my code in order to get the output for the whole class it would be very much appreciated. Thanks. CSV File

    public static void Main(string[] args)
    {
        string[] names = loadNames();
        int[] marks = loadMarks();

        outputMarks(names, marks);
        Console.ReadKey();
    }
    static string[] loadNames()
    {
        string filename = @"testMarks.csv";
        string[] tempData;
        string[] tempNames = new string[2];


        using (StreamReader currentfile = new StreamReader(filename))
        {
            tempData = currentfile.ReadLine().Split(',');
        }

        tempNames[0] = tempData[0];
        tempNames[1] = tempData[1];

        return tempNames;
    }
    static int[] loadMarks()
    {
        string filename = @"testMarks.csv";
        string[] tempData;


        using (StreamReader currentfile = new StreamReader(filename))
        {
            tempData = currentfile.ReadLine().Split(',');
        }
        int[] tempMarks = new int[tempData.Length - 2];

        for (int i = 2; i < tempData.Length; i  )
        {
            tempMarks[i - 2] = int.Parse(tempData[i]);
        }
        return tempMarks;
    }
    static void outputMarks(string[] names, int[] marks)
    {
        for (int i = 0; i < 2; i  )
        {
            Console.Write(names[i]   "\t");
        }

        for (int i = 0; i < 6; i  )
        {
            Console.Write(marks[i]   "\t");
        }
        Console.WriteLine();
    }
}

}

CodePudding user response:

Assuming the content of the testMarks.csv is like this (without empty lines ofcourse):

John,Adams,100,79,77,83,55,37

Sarah,Baxter,99,66,78,57,62,70

Jane,Clague,100,100,61,95,59,38

I would do this like :

public static void Main(string[] args)
{
    var allStudentsAndNotes = LoadStudentsAndNotes();
    Console.ReadKey();
}

  private List<Student> LoadStudentsAndNotes()
    {

        string pth = @"C:\MyDesktopPath\testMarks.csv";
        
        var lines = System.IO.File.ReadAllLines(pth);  

        var studentList = new List<Student>();
        Student student;
        foreach (var line in lines)
        {
            student = new Student();
            var datas = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            student.FirstName = datas[0];
            student.LastName = datas[1];
            for (int i = 2; i < datas.Length; i  )
            {
                student.Notes.Add(int.Parse(datas[i]));
            }

            studentList.Add(student);
        }
        return studentList;
    }

A little class

class Student
{
    public Student()
    {
        Notes = new List<int>();
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<int> Notes { get; set; }

}
  • Related