c# - Processing .txt files into arrays, and manipulating the array data -
i figure i'd post whole program i'm working with. i've pretty finished main method. need create 3 classes. fileroutines, student, , students. fileroutine main problem i'm running into. has 2 public classes. loadstudents, , loadassignments. load students supposed read of lines in student input file, create student object each line, , returns students object contains array of student objects. load assignments supposed read of lines in assignment input file , associate each assignment corresponding student based on student number. 2 text files 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 trying figure out how process 2 files, , make them want. told use
string[] lines = file.readalllines(path)
to read lines, ,
string[] data = line.split('\t')
to split tab delimited lines.
i'm not asking write program me. help. push in right direction. learn seeing something, , haven't seen way instructed this. if y'all have questions 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 path match save 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 student , assignment data text files. students students = fileroutines.loadstudents(student_file); fileroutines.loadassignments(assignment_file, students); // next, display information students , grades. console.writeline("the students , 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 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 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>"); } } } }
push in right direction...there's elegant solution parse file , create object list using linq. student list file can do:
public list<student> loadstudents(string student_file) { var parsequery = //read lines file line in file.readalllines(student_file) //split every line on tab delimiter let split = line.split(new[] {'\t'}) //create new student object string array created split every line select new student() { id = split[0], firstname = split[1], lastname = split[2] }; //execute parsequery, student objects in list<student> var studentlist = parsequery.tolist(); return studentlist; }
Comments
Post a Comment