用文本文件填充两个一维数组

Populating two one-dimensional arrays with text file

对于我的 Visual Basic 期末考试,我的程序需要将文本文件中的数据读取到两个不同的数组中,每个数组都是一维的。以下是我这样做的代码:

Option Explicit On
Option Infer Off
Option Strict On

Public Class frmMain

    'Constant for filename and a dirty flag variable
    Const INVENTORY_FILENAME As String = "inventory.txt"
    Dim noFile As Boolean = False

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Populates DVD listbox with text file data upon load

        'Variable for reading the file
        Dim myFile As IO.StreamReader

        'Declaring arrays for DVD names and prices
        Dim arrayDVD() As String
        Dim arrayPrice() As Double

        'Variables for populating arrays with respective data
        Dim dvdName As String
        Dim dvdPrice As Double
        Dim i As Integer = 0

        'Checking that file exists then reading data to each array
        If IO.File.Exists(INVENTORY_FILENAME) Then
            myFile = IO.File.OpenText(INVENTORY_FILENAME)

            'Read data to arrays
            Do Until myFile.Peek = -1
                dvdName = myFile.ReadLine()
                dvdPrice = Double.Parse(myFile.ReadLine())

                arrayDVD = dvdName
                arrayPrice = dvdPrice

                'Using arrays to populate multicolumn listbox
                lstDVD.Items.Add(arrayDVD(i) & arrayPrice(i))

                i += 1

            Loop

            'Closing the file
            myFile.Close()

        End If

    End Sub

End Class

文本文件将 DVD 的名称和价格替换为单独的行,使数组平行:

Pulp Fiction
9.99
Jumanji
13.99
And so on...

我收到一个值类型错误代码,指出在将数组的值设置为相等时我无法将 'String' 转换为 'String()' 或将 'Double' 转换为 'Double()'到他们各自的变量。有没有办法纠正这个问题?提前致谢!

这些行是错误的:

arrayDVD = dvdName
arrayPrice = dvdPrice

arrayDVDarrayPrice 数组 。您需要分配给每个数组中的 特定元素

arrayDVD(i) = dvdName
arrayPrice(i) = dvdPrice

不要忘记确保数组实际上有足够的元素。

提示:ReDim Preserve 几乎是确保数组足够大的最低效方法。每次使用都会分配一个全新的数组,一次复制一个元素,将新数组赋给旧引用,然后释放旧数组。它不会就地保存。不过,如果这是一门 100 级课程,这可能就是您此时应该做的事情。

最后,你应该永远不要在处理金钱时使用Double(改为使用Decimal)。


与问题分开,这里是我在没有奇怪的数组限制的情况下如何处理这个问题:

Private Iterator Function ReadInventoryFile(filePath As String) As IEnumerable(Of (String, Decimal))
    Using rdr As New StreamReader(filePath)
          Dim DVDName As String = Nothing
          While (DVDName = rdr.ReadLine()) IsNot Nothing
                Yield (DVDName, Decimal.Parse(rdr.ReadLine()))
          End While
    End Using
End Function

Const INVENTORY_FILENAME As String = "inventory.txt"
Private data As List(Of (String, Decimal))

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try 'Replaces the File.Exists() check
        data = ReadInventoryFile(INVENTORY_FILENAME).ToList()

        For Each item As (String, Decimal) In data
            lstDVD.Items.Add($"{item.Item1}{vbTab}{item.Item2:C}")
        Next
    Catch
        ' Actually do something here. Empty catch blocks are rarely correct.
        ' Note I catch at this level, rather than in the ReadFile() method.
    End Try
End Sub

您的代码的问题是您将数组设置为等于行,而不是数组内部的元素:

arrayDVD = dvdName
arrayPrice = dvdPrice

您只需将这些代码行替换为:

arrayDVD(i) = dvdName
arrayPrice(i) = dvdPrice

但是,还有一个问题是你的数组在初始化时没有定义长度。

由于数组长度是相对于文件行数的,你可以读取文件两次:一次检查文件行数,然后用长度的一半初始化每个数组,然后第二次将行添加到数组中。