0

I figure I'd just post the whole program I'm working with. I've pretty much finished the Main method. I just need to create 3 classes. FileRoutines, Student, and Students. FileRoutine is the main problem I'm running into. It has 2 public classes. LoadStudents, and LoadAssignments. Load Students is supposed to read all of the lines in the student input file, create a Student object from each line, and returns a Students object that contains an array of the Student objects. Load Assignments is supposed to read all of the lines in the assignment input file and associate each assignment with the corresponding student based on the student number. The 2 text files look like this:

Students.txt

122338  Weltzer Teresa
123123  Wang    Choo
123131  Adams   James
123132  Binkley Joshua
123139  Howard  Tyler
123160  King    Alma    

Assignments.txt

122338  HW1 93  0.05
123123  HW1 100 0.05
123131  HW1 90  0.05
123132  HW1 85  0.05
123139  HW1 89  0.05
123160  HW1 90  0.05
122338  Quiz1   94  0.08
123123  Quiz1   83  0.08
123131  Quiz1   93  0.08
123132  Quiz1   72  0.08
123139  Quiz1   0   0.08
123160  Quiz1   63  0.08
122338  HW2 70  0.05
123123  HW2 95  0.05
123131  HW2 100 0.05
123132  HW2 82  0.05
123139  HW2 73  0.05
123160  HW2 81  0.05
122338  HW3 87  0.05
123123  HW3 98  0.05
123131  HW3 87  0.05
123132  HW3 65  0.05
123139  HW3 55  0.05
123160  HW3 81  0.05
122338  Test1   86  0.15
123123  Test1   82  0.15
123131  Test1   92  0.15
123132  Test1   78  0.15
123139  Test1   67  0.15
123160  Test1   74  0.15

I'm really only trying to figure out how to process those 2 files, and make them do what I want. I was told to use

string[] lines = File.ReadAllLines(path)

to read the lines, and

string[] data = line.Split('\t') 

to split the tab delimited lines.

I'm not asking for someone to write this program for me. Just for some help. A push in the right direction. I really learn by seeing something, and I haven't really seen anyone do something like the way I was instructed to do this. If y'all have any questions to ask, feel free to!

Thanks in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Project2
{
    class Program
    {
        static void Main()
        {
            // Change the path to match where you save the files.
            //const string STUDENT_FILE = @"C:\Temp\Students.txt";
            //const string ASSIGNMENT_FILE = @"C:\Temp\Assignments.txt";
            const string STUDENT_FILE = @"C:\Users\PleaseGod\Desktop\Project 2\Students.txt";
            const string ASSIGNMENT_FILE = @"C:\Users\PleaseGod\Desktop\Project 2\Assignments.txt";

            // First, load the student and assignment data from text files.
            Students students = FileRoutines.LoadStudents(STUDENT_FILE);
            FileRoutines.LoadAssignments(ASSIGNMENT_FILE, students);

            // Next, display information about the students and grades.
            Console.WriteLine("The students and their average are:");
            foreach (Student student in students.StudentList)
            {
                Console.WriteLine("{0,-20}\t{1:N2}", student.FullName, student.GetAverage());
            }

            DisplayStudentsByGrade(students, LetterGrade.A);
            DisplayStudentsByGrade(students, LetterGrade.B);
            DisplayStudentsByGrade(students, LetterGrade.C);
            DisplayStudentsByGrade(students, LetterGrade.D);
            DisplayStudentsByGrade(students, LetterGrade.F);

            Student[] studentsWithHighestAverage = students.GetStudentsWithHighestAverage();
            Console.WriteLine("\nThe students with the highest average are:");
            foreach (Student student in studentsWithHighestAverage)
            {
                Console.WriteLine("{0,-20}\t{1:N2}", student.FullName, student.GetAverage());
            }

            Console.WriteLine("\nThe class average is: {0:N2}", students.GetClassAverage());

            Console.WriteLine("\nThe class average on HW2 is: {0:N2}", students.GetAverageGradeOnAssignment("HW2"));

            Console.ReadLine();
        }

        private static void DisplayStudentsByGrade(Students students, LetterGrade letterGrade)
        {
            Student[] studentList = students.GetStudentsByGrade(letterGrade);

            Console.WriteLine("\nThe following students have a grade of {0}:", letterGrade);
            if (studentList.Length > 0)
            {
                foreach (Student student in studentList)
                {
                    Console.WriteLine("{0,-20}\t{1:N2}", student.FullName, student.GetAverage());
                }
            }
            else
            {
                Console.WriteLine("<none>");
            }
        }
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Adam Cole
  • 1
  • 1
  • 1
    What's the actual question? – devdigital May 04 '13 at 13:20
  • Can you please rephrase the question to make what you need more obvious? There is too much of information here. As I can see, you already got answer by using File.ReadAllLines & String.split. that is one way to approach. What more do you want to acheive by this question? – Buddha May 04 '13 at 13:26
  • I honestly am not completely sure of a question. I was taking a c# class at my school, and I got a pretty bad virus half way through the semester. I've already failed the class, but I'm just going over some of the material seeing if I can understand how to do some of these things for the next time that I take it. But if I was to be asking a question, it'd be how do I get started with all of this? – Adam Cole May 04 '13 at 13:47
  • I understand that's a bland question, but it's the best I can think of. – Adam Cole May 04 '13 at 13:49

3 Answers3

1

Push in the right direction...There's an elegant solution to parse file and create object list using LINQ. To get your student list from file you can do:

public List<Student> LoadStudents(string STUDENT_FILE)
{
   var parseQuery = //read all lines from a file
                 from line in File.ReadAllLines(STUDENT_FILE)
                 //split every line on Tab delimiter
                 let split = line.Split(new[] {'\t'})
                 //create new Student object from string array created by split for every line
                 select new Student()
                 {
                   ID = split[0],
                   FirstName = split[1],
                   LastName = split[2]
                 };

     //execute parseQuery, your student objects are now in List<Student>
      var studentList = parseQuery.ToList();
      return studentList;
 }
Jurica Smircic
  • 6,117
  • 2
  • 22
  • 27
  • Do you have a way I can talk to you in a more constant fashion? Like Skype, or something? I wouldn't mind a quick tutor, or someone to help me that I can actually get to somewhat know. – Adam Cole May 04 '13 at 13:45
  • I guess that's not possible, haha. – Adam Cole May 04 '13 at 14:23
  • @AdamCole I'm sorry. I do try to contribute in the developer communitiy and answer some question in my free time. But I do have daily job and time for this is limited. Myabe you can try on the chat here on SO and get some more tutoring.. – Jurica Smircic May 04 '13 at 14:41
0

Its not clear what you are trying to achieve, but from what i understood you want to know how to read file, then you can use StreamReader. there's alot of questions here about this topic already you can search how to read txt file.

Community
  • 1
  • 1
ma1169
  • 659
  • 1
  • 8
  • 26
  • I've seen that there are multiple ways to go about reading it, but the instructions that were given kind of hinted to do it this way. It's a basic c# class/book, so I figure it'd be better for me to learn it this way before branching out. – Adam Cole May 04 '13 at 13:48
  • if you want to know how to use ReadAllLines here's example [MSDN ReadAllLines](http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx) beside try to be specific like saying clearly and specific what you want to do but you don't know how to achieve it – ma1169 May 04 '13 at 15:52
0

Try something like:

class Student
{
    public string mNumber { get; set; }
    public string mFirstName { get; set; }
    public string mLastName { get; set; }

    public Student(string nr, string fName, string lName)
    {
        mNumber = nr;
        mFirstName = fName;
        mLastName = lName;
    }
}

and loop over the data, like:

foreach (string val in data)
{
    string[] valArr = val.Split(' ');
    Student st = new Student(valArr[0], valArr[1], valArr[2]);
}
Gerard
  • 2,649
  • 1
  • 28
  • 46