954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Importing a text file into multiple variables.

Hi
Am new to this and was wondering if anyone knew a solution to this problem.

I am wanting to get information from a text file into different arrays.

My current text file has a new line for each driver but I would like it to look something like this:

Hamilton,Button,Alonso,Massa,Webber,Vettel
Di Resta,Sutil,Rosberg,Perez,Buemi,Glock

Each line of the text file represents a player and each player has 6 drivers.
Each driver is seperated by a comma (,) which is what I need the program to understand.

So for example I would want to import 'player one' drivers into the array:
playerdrivers(1,1) - playerdrivers(1,6)
So (1,1) would be Hamilton (1,2) would be Button and so on.
Player two and further players would have a different first number e.g (2,1) (3,1)

This code I have written but does not work as it tries to convert from string to double.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim playerdrivers(4, 6) As Array
        Dim destination As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/f1data/bin/array.txt"
        Dim FileReader1 As New StreamReader(File.Open(destination, FileMode.OpenOrCreate))
        Dim Contents1 As String
For player = 1 To 4
        For driver = 1 To 6
        Contents1 = FileReader1.ReadLine
            playerdrivers(player, driver) = Contents1
        Next
Next
        FileReader1.Close()


Please could you help me by suggesting any improvements or how to get past this error.

djjavo
Light Poster
28 posts since Jun 2011
Reputation Points: 10
Solved Threads: 1
 

Yes, I would revise that code into something like this.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Dim playerdrivers As New ArrayList
   Dim destination As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\f1data\bin\array.txt"
   Dim FileReader1 As New StreamReader(destination)
   Dim Contents1 As String
   Dim index As Integer = 0

   While FileReader1.Peek <> -1
      Contents1 = FileReader1.ReadLine
      Dim array As New ArrayList
      array.AddRange(Contents1.Split(","))
      playerdrivers.Add(array)
   End While

   FileReader1.Close()

After that you can easily access each player and driver usingalmost the same method as you intended:
playerdrivers(0).Item(0) - playerdrivers(0).Item(5)
playerdrivers(1).Item(0) - playerdrivers(1).Item(5)

Oxiegen
Master Poster
715 posts since Jun 2006
Reputation Points: 87
Solved Threads: 141
 

That works a dream.
Thank you very much, it is much appreciated :)

djjavo
Light Poster
28 posts since Jun 2011
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: