在视觉基础中拆分后求和双精度



我正在从如下所示的 txt 文件中提取数据

姓名,123

   Dim total As Double = 0
    For Each line As String In IO.File.ReadAllLines("file path")
        'Dim t As String() = line.Split(New Char() {","c})
        Dim parts As String() = line.Split(New Char() {","c})
        Dim firstPart As String = parts(1)
        total += Double.Parse(firstPart)

它说零件(1)超出了范围。任何帮助将不胜感激

显然,

其中一行要么不包含分隔符,要么根本不包含文本。

您应该添加一个检查以忽略这些行:

If parts.Length < 2 Then Continue For 'There are not enough parts. Continue with the next line instead.
Dim firstPart As String = parts(1)
total += Double.Parse(firstPart)
-

Continue声明 - MSDN

第一部分将是 parts(0),因为数组被枚举为 0 到 N.. 但无论如何。您还应该测试是否有空行。后者通常是以新行结尾的文本文件的问题。

最新更新