importing txt file and manipulating string to do calculations?

喜你入骨 提交于 2020-01-06 14:16:21

问题


In VB2010 I need to process a text file which has multiple lines as follows:

Student name, 65, 42, 71, 55
Student name 2, 40, 42, 54, 45
etc.

I need to process the file, finding:

  1. the overall end of year mark and grade each user received
  2. the student who got the highest mark
  3. the average overall mark
  4. the list of students who achieved a particular grade that the user can specify.

Grades: 70% - 100% = D, 60% - 70% = P1, 50% - 60% = P2, 40% - 50% = P3, 0% - 39% = F

I am trying to put a data structure into Public Class

Structure Student
    Dim name as String
    Dim Paper1 as String
    Dim Paper2 as String
    Dim Paper3 as String
    DIm Paper4 as String.
End Structure

'I am also trying to to do an array
Dim Students() as Student

'Open txt File

Dim FileStreamReader As IO.StreamReader
FileStreamReader = IO.File.OpenText("c:/textfile.txt")

Read the File one line at a time

While not fileStreamReader.EndofStream
    Dim linein as String
    linein = FileStreamReader.readline()

I have two main questions:

  1. Am I on the right track and if so,
  2. Any ideas how I can go about manipulating the information from the txt file? I have been trying for days but I keep getting errors!

回答1:


You should be using a dedicated csv parser to parse lines in the file. Do not use regular expressions. Do not use String.Split(). Do not try to write your own. You can use the Microsoft.VisualBasic.TextFieldParser class.

You should not use a struct with fields that can be changed. Make it a class instead of a Structure, or make it all the fields read only (no public Set option) and only change the fields in the constructor.



来源:https://stackoverflow.com/questions/15621756/importing-txt-file-and-manipulating-string-to-do-calculations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!