【问题标题】:Populating an array from an input file从输入文件填充数组
【发布时间】:2019-11-20 01:21:59
【问题描述】:

我是 vb 和一般编码的极端初学者,这是我在这个网站上的第一篇文章。我正在为我正在做的一个项目而碰壁。这是项目中最小的代码块,但所有其他函数将从我要填充的数组中提取。本质上,我需要用代表 .txt 文件中 DVD 价格的数字填充一个数组。 .txt 文件格式如下:

指环王,10.50
阿凡达,5
纽约帮派,7.5
等等

其中 10.50 是我想要分配给 dblPrices(0) 的值。要求不更改 .txt 文件的格式。到目前为止,这是我使用的,但是在测试输出时我得到了 0:

    'Declare variables.
    Dim intCount As Integer = 0
    Dim strLine As String

    'Open the file for input.
    inFile = IO.File.OpenText("availableDVDs.txt")

    'Remove alpha characters from string, assign numeric values to array representing price.
    Do Until inFile.Peek = -1
        strLine = inFile.ReadLine.ToUpper.Replace("[A-Z]", "")
        strLine = strLine.Replace(" ", "")
        strLine = strLine.Replace(",", "")
        Double.TryParse(strLine, dblPrices(intCount))
        intCount += 1
    Loop

这与学校项目有关,所以我不一定要找人为我做我的工作,但也许会为我指明正确的方向。谢谢!

【问题讨论】:

  • 以逗号分割,对第二个元素执行 TryParse。
  • 我试过了,事实上这是我的第一个想法。但是我们需要有 Option Strict On 所以我得到一个从“String”到“Char”的隐式转换错误
  • 如果您显示该代码,我们可以解决此问题。
  • dim values = strLine.Split(","c) dim value As Decimal = 0.0D If Decimal.TryParse(values(1), NumberStyles.Currency, CultureInfo.InvariantCulture, value) Then dblPrices(intCount) = value end if 。您应该使用List(Of Decimal)。你怎么知道你会在那个文件中找到多少行?数组的维度是怎么设置的?
  • 我们被允许硬编码数组的维度。

标签: arrays vb.net


【解决方案1】:

如果您使用List(Of T)(T 代表Type,如IntegerString 或您自己的类型),那么您不需要像数组一样提前知道大小。此外,您不需要跟踪索引。您可以只使用.Add 方法并将新项目放在列表的末尾。

您可以先使用File.ReadAllLines 从文件中获取数据,这将在文本文件中返回一个行数组。然后您可以在“,”(逗号)上的每一行 .Split 并使用结果数组的第二个元素。双引号中逗号后的小 c 告诉编译器您的意思是这是一个 Char ,它是 .Split 所期望的数据类型。

我使用了Decimal 数据类型而不是Double。与金钱打交道时,使用它来获得您期望的答案会更安全。

在第二个代码示例中,我使用了由字符串前面的 $ 指示的插值字符串。请注意,它与String.Format 非常相似,只是变量直接插入大括号而不是占位符。这在 Visual Studio 2015 及更高版本中可用。

数组方法...

Private Sub OPCode()
    Dim Lines = IO.File.ReadAllLines("availableDVDs.txt")
    Dim Prices(Lines.Count - 1) As Decimal
    Dim index As Integer
    Dim price As Decimal
    For Each line In Lines
        Dim SplitOnComma = line.Split(","c)
        If Decimal.TryParse(SplitOnComma(1), price) Then
            Prices(index) = price
            index += 1
        Else
            MessageBox.Show(String.Format("Price for {0} is not valid.", SplitOnComma(0)))
        End If
    Next
End Sub

列表方法...

Private Sub UsingList()
    Dim Lines = IO.File.ReadAllLines("availableDVDs.txt")
    Dim Prices As New List(Of Decimal)
    Dim price As Decimal
    For Each line In Lines
        Dim SplitOnComma = line.Split(","c)
        If Decimal.TryParse(SplitOnComma(1), price) Then
            Prices.Add(price)
        Else
            MessageBox.Show($"Price for {SplitOnComma(0)} is not valid.")
        End If
    Next
End Sub

选项推断关闭

Private Sub OPCode()
    Dim Lines() As String = IO.File.ReadAllLines("availableDVDs.txt")
    Dim Prices(Lines.Count - 1) As Decimal
    Dim index As Integer
    Dim price As Decimal
    For Each line In Lines
        Dim SplitOnComma() As String = line.Split(","c)
        If Decimal.TryParse(SplitOnComma(1), price) Then
            Prices(index) = price
            index += 1
        Else
            MessageBox.Show(String.Format("Price for {0} is not valid.", SplitOnComma(0)))
        End If
    Next
End Sub

【讨论】:

  • 我已在我的答案中添加了代码以涵盖 Option Infer off。当你被允许打开它时,你会看到编辑器已经知道类型,所以你不需要添加它。
  • 嗨,玛丽,感谢您的回复。我对实现数组方法很感兴趣,但我遇到了一些错误。我需要启用 Option Strict,并且我收到以下错误“Option Strict On 不允许后期绑定”的多个实例。此外,“Option Strict on”的几个实例要求所有变量声明都有一个“As”语句。最后,在每次提到“line”时,都会出现“line is not declared”错误。有什么建议吗?
  • 我关闭所有选项只是为了测试,我得到一个未处理的异常:System.MissingMemberException: 'Public member 'Count' on type 'String()' not found.'
  • 将 .Count 更改为 .Length
  • 你需要 Option Explicit 和 Option Strict 来保持你的代码干净。打开这两个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
  • 2018-05-22
  • 1970-01-01
相关资源
最近更新 更多