【发布时间】: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)。你怎么知道你会在那个文件中找到多少行?数组的维度是怎么设置的? -
我们被允许硬编码数组的维度。