Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey!

I need help understanding how to write the code to parse this data (below) from a .txt file and store them as variables

Kim,83,Junior
Jake,76,Senior
Erin,98,Senior
Ronald,69,Senior
Jim,83,Freshman
Kate,91,Sophomore
Steve,64,Freshman
Julie,89,Junior
Mark,75,Senior
Paul,81,Senior
Mike,56,Freshman
Meghan,97,Junior
Jason,67,Sophomore
Ling,88,Senior
Lori,70,Freshman

I believe that I have to use some of this code?

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sr As IO.StreamReader = IO.File.OpenText("input.txt")
        Dim strinput As String


but am not sure what else to do. Thank you!

[edit]code block added[/edit]
Posted
Updated 11-Nov-12 11:44am
v2
Comments
Sergey Alexandrovich Kryukov 11-Nov-12 23:21pm    
What did you try so far?
--SA

This is covered in your class materials.

You read each line of the file
split the line on ","
process the data as necessary
repeat until there are no more lines.

That's the best you're going to get. Nobody here is going to write your homework for you.
 
Share this answer
 
Better use the class System.IO.StreamReader: http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^].

Define enumeration type like enum Stage = { Freshman, Junior, Senior, ... }, than create a structure or a class with three fields and constructor with three parameters: string name, integer… I don't know what, whatever 83, 76, 98, etc. mean, and Stage. Then, declare some collection with the items of this type, for example, System.Collections.Generic<>.

Read the file line by line, split each time in three strings using string.Split by ',', and parse integer value using int.Parse or int.TryParse (or uint.Parse, uint.TryParse) and enum using enum.Parse:
http://msdn.microsoft.com/en-us/library/system.string.split.aspx[^],
http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx[^],
http://msdn.microsoft.com/en-us/library/system.uint32.parse%28v=vs.71%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx[^].

For each line, create an instance of your structure or class out of parsed data and add to the collection.

Next time, try to solve such simple problems by yourself, learn how to find in MSDN help pages all you need.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900