1

I have a text file called games.txt that contains the details of many computer game matches. Each line represents:

Player1Score, Player1Name, Player1Age, Player 2Score, Player2Name, Player2Age 

Here is an example of 3 lines (there are a total of 150):

1, John, 32, 5, Albert, 54
3 Lisa, 33, 2, Michael, 36
4 Jessica, 24, 1 Robert, 32

I want to assign them to a structure array in which each element has 6 member variables using StreamReader. Here is the structure variable:

Structure gameDetails
    Public Player1Score As Integer
    Public Player1Name As String
    Public Player1Age As Integer
    Public Player2Score As Integer
    Public Player2Name As String
    Public Player2Age As Integer
End Structure

I know that using a comma as a delimiter, I can record each variable as a different element in the array:

Dim game() as String
game() = inFile.ReadLine.Split(","c)

which would result in each element being assigned in this way:

game(0) = 1

game(1) = John

game(2) = 32

Etc..

Is there a way I can directly assign all the member variables to each element? Like maybe each comma separates a member variable, and a new line means a new element? Unfortunately, I am not familiar with the syntax/commands so I do not know how to approach this.

Note that I cannot hard-code anything as the application is designed to take all the data from the .txt.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
leah
  • 21
  • 4
  • Just commenting on a typo, 2 commas are missing in the example lines in the game.txt. – leah May 11 '15 at 23:33

2 Answers2

0

Could you try creating a method as part of your structure that goes something like this:

public Sub MapLineEntry(targetLine() As String)
    Player1Score = CInt(targetLine(0))
    Player1Name = targetLine(1)
    Player1Age = CInt(targetLine(2))
    Player2Score = CInt(targetLine(3))
    Player2Name = targetLine(4)
    Player2Age = CInt(targetLine(5))
End Sub

You would want to make sure that the string array has the correct number of elements, but this would be a start.

ChicagoMike
  • 618
  • 3
  • 11
0

just wanted to say I figured out a method although I'm not sure how efficient it is. I ended out putting each part of the each line (separated by commas) onto an array. Then, I assigned the array to each of the member variables. Then I looped it to do it for every single variable:

Dim inFile As System.IO.StreamReader
    inFile = New IO.StreamReader("Games.txt")
    If IO.File.Exists("Games.txt") Then

        Dim upperbound As Integer = Games.GetUpperBound(0)

        For i As Integer = 0 To upperbound
            'auxilary string
            Dim line(6) As String
            line = inFile.ReadLine.Split(","c)

            Games(i).Player1Score = CInt(line(0))
            Games(i).player1Name = line(1)
            Games(i).player1Age = CInt(line(2))
            Games(i).player2Score = CInt(line(3))
            Games(i).player2Name = line(4)
            Games(i).player2Age = CInt(line(5))
        Next i
    End If

Thank you for the help!

leah
  • 21
  • 4