Results 1 to 10 of 10

Thread: Splitting File and Adding to a New List

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    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

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Splitting File and Adding to a New List

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         If System.IO.File.Exists("c:\test.txt") Then
    5.             Dim strTemp() As String = System.IO.File.ReadAllLines("c:\test.txt")    '~~~ Read all lines
    6.  
    7.             '~~~ Loop through the lines
    8.             For Each strLine As String In strTemp
    9.                 Dim a() As String
    10.  
    11.                 a = strLine.Split(","c) '~~~ Split it
    12.  
    13.                 '~~~ Display it
    14.                 Debug.Print(a(0))
    15.                 Debug.Print(a(1))
    16.                 Debug.Print(a(2))
    17.                 Debug.Print(a(3))
    18.                 Debug.Print(a(4))
    19.                 Debug.Print(a(5))
    20.             Next
    21.  
    22.         End If
    23.     End Sub
    24. 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,...

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Splitting File and Adding to a New List

    using your space delimited text from post #1:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim rseat As New List(Of Integer)  'list of seat numbers
    4.     Dim rname As New List(Of String)   'list of ticket holders
    5.     Dim rcost As New List(Of Double)   'cost of seat
    6.  
    7.     Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
    8.         Dim lines() As String = IO.File.ReadAllLines(txtFileName.Text)
    9.         For y As Integer = 0 To lines.GetUpperBound(0)
    10.             Dim fields() As Object = lines(y).Split(" "c)
    11.             rseat.Add(CInt(fields(1)))
    12.             rname.Add(fields(3).ToString)
    13.             rcost.Add(CDbl(fields(5)))
    14.         Next
    15.     End Sub
    16.  
    17. End Class

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    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!

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Splitting File and Adding to a New List

    you'd just change the .split:

    Dim fields() As Object = lines(y).Split(","c)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    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.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Splitting File and Adding to a New List

    sorry i didn't notice your actual file format:

    vb Code:
    1. Dim fields() As Object = lines(y).Split(","c)
    2. rseat.Add(CInt(fields(0)))
    3. rname.Add(fields(1).ToString)
    4. rcost.Add(CDbl(fields(2)))

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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

  10. #10
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Splitting File and Adding to a New List

    Same as .paul.'s code:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim rseat As New List(Of Integer)  'list of seat numbers
    4.     Dim rname As New List(Of String)   'list of ticket holders
    5.     Dim rcost As New List(Of Double)   'cost of seat
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         If System.IO.File.Exists("c:\test.txt") Then
    9.             Dim strTemp() As String = System.IO.File.ReadAllLines("c:\test.txt")    '~~~ Read all lines
    10.  
    11.             '~~~ Loop through the lines
    12.             For Each strLine As String In strTemp
    13.                 Dim a() As String
    14.  
    15.                 a = strLine.Split(","c) '~~~ Split it
    16.  
    17.                 '~~~ Save it
    18.                 rcost.Add(CDbl(a(2)))
    19.                 rname.Add(a(1))
    20.                 rseat.Add(CInt(a(0)))
    21.             Next
    22.  
    23.         End If
    24.     End Sub
    25. 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
  •  



Click Here to Expand Forum to Full Width