Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VBVB.NETParsing, +
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?
 
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 11-Nov-12 11:43am
Edited 11-Nov-12 11:44am
Nelek42.5K
v2
Comments
Sergey Alexandrovich Kryukov at 11-Nov-12 23:21pm
   
What did you try so far? --SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 838
1 OriginalGriff 461
2 Tadit Dash 355
3 sanket saxena 329
4 Peter Leow 228
0 Sergey Alexandrovich Kryukov 11,990
1 OriginalGriff 7,349
2 Peter Leow 5,003
3 Abhinav S 4,003
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 11 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid