文本文件到Multidimensinal数组Vb.net

时间:2016-07-13 19:05:18

标签: vb.net multidimensional-array text-files vb.net-2010

所以我正在开发一个跟踪团队投注的程序......论文是一些变量

Dim dataArray(9, 6) As Double
Dim betTypeMATH As Integer
Dim betTypeStrings="Favorite", "Under", "Pick'em", "Over", "Under", "Middle", "Even", "Teaser", "Parlay"}

基本上我的代码现在我让它显示正确并正确地写入文本文件

写入我使用的文件这只能确保数组中的数字以以下方式保存:(下面的内容)

     Console.WriteLine()
    Console.WriteLine("1 -- What is the new name of the new report?")
    NewRepName = Console.ReadLine()
    FileOpen(1, NewRepName & ".txt", OpenMode.Output) 'Creates the textfile
      Dim ReportCreation As String = Convert.ToString(dataArray)
                For i = 0 To 8
                    For j = 0 To 5
                        Print(1, dataArray(i, j))
                    Next
                    Print(1, "  " & vbCrLf)
                Next

2 0 1 239 0 239
 0 0 0 0 0 0
 0 0 0 0 0 0
 0 0 0 0 0 0
 1 1 0.5 56 -56 0
 0 0 0 0 0 0
 0 0 0 0 0 0
 0 0 0 0 0 0
 2 0 1 260 0 260

要显示我格式化的数组,以便它在控制台上很好地显示我写了一个子程序

Sub displayArray()
    Console.Clear()

    'thirteen charachters between the beginning of the lines

    'Console.WindowWidth = 100

    Console.WriteLine()
    Console.WriteLine("Bet Type     # Wins       # Losses     % Wins       $ Won        $Loss        $ Net W/L")
    Console.WriteLine("========     ========     ========     ========     =========    ========     =========")



    For i As Integer = 0 To 8
        Console.WriteLine()
        Console.WriteLine(betTypeStrings(i))

        For j As Integer = 0 To 5

            Console.Write(dataArray(i, j))

        Next

    Next

显示侧面的每个投注类型名称以及dataArray的每个坐标与其编号对应 所以喜欢上面会显示上面显示的文本文件

投注类型#Wins #Lesses%赢得$ Won $ Loss $ Net W / L

======== ======== ======== ======== ========= ====== == =========
2 0 1 239 0 239 喜爱

当然更多调整.... 我在如何从文本文件中获取每个数字到dataArray

时遇到了问题

我想出了这个

    Using fileReader = New StreamReader(RepName)
        Dim numline As String = ""
        Dim num As Double = 0
        Dim dataBets(9, 6) As Double
        Dim i As Integer = 0
        Dim j As Integer = 0

        Do Until fileReader.Peek = -1
            numline = fileReader.ReadLine()

但是这里我被困在了......

更新:现在我在以下代码中遇到错误:

         Using fileReader = New StreamReader(RepName)
        Dim dataBets(9, 6) As Double

        For y As Integer = 0 To 8 'loop through each line of the data (expects exactly 9 lines)
            Dim numline As String = fileReader.ReadLine() 'read a line from the file
            Dim parts() As String = numline.Split(" "c) 'split the line in to an array of strings on spaces
            For x As Integer = 0 To 5 'loop through 6 columns
                Dim value As Double = Convert.ToDouble(parts(x)) 'read the column and turn it back in to an integer
                dataBets(x, y) = value 'stuff it back in to the array
            Next
        Next

使用行:Dim值As Double = Convert.ToDouble(parts(x))或Dim值As Double = Integer.parse(parts(x)) 出现以下错误“mscorlib.dll中发生了'System.FormatException'类型的未处理异常附加信息:输入字符串的格式不正确。”

1 个答案:

答案 0 :(得分:0)

类似于这个伪代码的东西应该有用。 它采用非常严格的文件格式,因此您可能需要添加更多错误检查以确保其有效。 它循环通过9行6列,为每行读取一个字符串,并将其拆分为空格。它从当前列的行中获取一个分割值,将其转换为整数,并放置在当前数组位置。

for y as integer = 0 to 8 'loop through each line of the data (expects exactly 9 lines)
    dim numline as string = filereader.ReadLine() 'read a line from the file
    dim parts() as string = numLine.Split(" "c) 'split the line in to an array of strings on spaces
    for x as integer = 0 to 5 'loop through 6 columns
        dim value as integer = integer.parse(parts(x)) 'read the column and turn it back in to an integer
        dataBets(x,y) = value 'stuff it back in to the array
    next
next