|
-
Mar 22nd, 2011, 10:39 AM
#1
Thread Starter
Junior Member
Splitting File and Adding to a New List
Hey everyone! Im trying to load data from a file into three parallel lists, each piece of data is a different data type. Using a split at the "comma", im having trouble trying to load each piece of data in each list. These parallel lists can be like this:
Index rseat index rname index rcost
0 123 0 Saleh 0 20.00
1 124 1 Saleh 1 20.00
2 544 2 Chandra 2 15.00
3 322 3 Quinn 3 15.00
Etc. etc. etc.
This is the code I got so far
Code:
Public Class Form1
Dim ticketFile As System.IO.StreamReader
Dim rseat As New List(Of Integer) 'list of seat numbers
Dim rname As New List(Of String) 'list of ticket holders
Dim rcost As New List(Of Double) 'cost of seat
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim count As Integer = 0
Dim record As String
'this logic tests for file exists before attempting to open it
If System.IO.File.Exists(txtFileName.Text) Then
'this statement opens the file and instantiates a file object
ticketFile = New System.IO.StreamReader(txtFileName.Text)
Do While ticketFile.Peek <> -1
record = ticketFile.ReadLine
ProcessRecord(record)
count += 1
Loop
lblCount.Text = "found " & count.ToString & " reserved seats"
Else
lblCount.Text = "file not found"
End If
End Sub
Private Sub ProcessRecord(ByVal rec As String)
Dim recField() As String
'split will break a long string like rec into an array of
'shorter strings (recfield) using a delimiter like ","
recField = Split(rec, ",")
rcost = recField(2)
rname = recField(1)
rseat = recField(0)
End Sub
-
Mar 22nd, 2011, 10:46 AM
#2
Re: Splitting File and Adding to a New List
Hi...
im having trouble trying to load each piece of data in each list.
What is that ?
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 22nd, 2011, 10:53 AM
#3
Re: Splitting File and Adding to a New List
vb.net Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If System.IO.File.Exists("c:\test.txt") Then
Dim strTemp() As String = System.IO.File.ReadAllLines("c:\test.txt") '~~~ Read all lines
'~~~ Loop through the lines
For Each strLine As String In strTemp
Dim a() As String
a = strLine.Split(","c) '~~~ Split it
'~~~ Display it
Debug.Print(a(0))
Debug.Print(a(1))
Debug.Print(a(2))
Debug.Print(a(3))
Debug.Print(a(4))
Debug.Print(a(5))
Next
End If
End Sub
End Class
File contents:
Code:
0,123,0,Saleh,0,20.00
1,124,1,Saleh,1,20.00
2,544,2,Chandra,2,15.00
3,322,3,Quinn,3,15.00
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 22nd, 2011, 11:10 AM
#4
Re: Splitting File and Adding to a New List
using your space delimited text from post #1:
vb Code:
Public Class Form1
Dim rseat As New List(Of Integer) 'list of seat numbers
Dim rname As New List(Of String) 'list of ticket holders
Dim rcost As New List(Of Double) 'cost of seat
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim lines() As String = IO.File.ReadAllLines(txtFileName.Text)
For y As Integer = 0 To lines.GetUpperBound(0)
Dim fields() As Object = lines(y).Split(" "c)
rseat.Add(CInt(fields(1)))
rname.Add(fields(3).ToString)
rcost.Add(CDbl(fields(5)))
Next
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 22nd, 2011, 02:10 PM
#5
Thread Starter
Junior Member
Re: Splitting File and Adding to a New List
Sorry i know it everything might sound confusing.
Akhileshbc, im trying to load each of the piece of data into 3 different lists... rseat hold all of the 3 digit seat numbers, rname holds the name, rcost holds the cost. So each time the loop runs its adds the 1 of 3 data piece into each different list. Ultimately im trying to load the data into the lists, then the user will search a name and it will search the list for the same name, then load the corresponding data in a label. Say if you searched the name Saleh, the label would read:
123 Saleh 20.00
124 Saleh 20.00
Paul, again sorry for the confusion. The text file actually looks like this, so instead of a space its a comma:
123,Saleh,20.00
124,Saleh,20.00
544,Chandra,15.00
322,Quinn,15.00
323,Quinn,15.00
543,Chandra,15.00
222,Bazouzi,17.50
942,Slothrop,9.99
Why im making this super hard is the fact that i want to load it into lists, so i dont have to open and close the file everytime i do a search, and hopefully cut down on search time for bigger lists. Thanks for all the help!
-
Mar 22nd, 2011, 02:29 PM
#6
Re: Splitting File and Adding to a New List
you'd just change the .split:
Dim fields() As Object = lines(y).Split(","c)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 22nd, 2011, 02:59 PM
#7
Thread Starter
Junior Member
Re: Splitting File and Adding to a New List
1. Dim fields() As Object = lines(y).Split(" "c)
2. rseat.Add(CInt(fields(1)))
3. rname.Add(fields(3).ToString)
4. rcost.Add(CDbl(fields(5)))
At line 2 I get the error message "Conversion from string "Saleh" to type 'Integer' is not valid."
Its trying to add the name as the first value yet in the text file, the numbers are first.
-
Mar 22nd, 2011, 03:13 PM
#8
Re: Splitting File and Adding to a New List
sorry i didn't notice your actual file format:
vb Code:
Dim fields() As Object = lines(y).Split(","c)
rseat.Add(CInt(fields(0)))
rname.Add(fields(1).ToString)
rcost.Add(CDbl(fields(2)))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 22nd, 2011, 03:15 PM
#9
Re: Splitting File and Adding to a New List
you need to be sure that your file contents are exactly that:
integer,string,double
+ no empty lines or it'll throw an error
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 23rd, 2011, 03:34 AM
#10
Re: Splitting File and Adding to a New List
Same as .paul.'s code:
vb.net Code:
Public Class Form1
Dim rseat As New List(Of Integer) 'list of seat numbers
Dim rname As New List(Of String) 'list of ticket holders
Dim rcost As New List(Of Double) 'cost of seat
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If System.IO.File.Exists("c:\test.txt") Then
Dim strTemp() As String = System.IO.File.ReadAllLines("c:\test.txt") '~~~ Read all lines
'~~~ Loop through the lines
For Each strLine As String In strTemp
Dim a() As String
a = strLine.Split(","c) '~~~ Split it
'~~~ Save it
rcost.Add(CDbl(a(2)))
rname.Add(a(1))
rseat.Add(CInt(a(0)))
Next
End If
End Sub
End Class
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|