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.

Recommended Answers

All 2 Replies

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 using almost the same method as you intended:
playerdrivers(0).Item(0) - playerdrivers(0).Item(5)
playerdrivers(1).Item(0) - playerdrivers(1).Item(5)

commented: Very helpful. +0

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.