繁体   English   中英

在Visual Basic中拆分后求和

[英]Sum a double after split in visual basic

我正在从看起来像这样的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)

就是说parts(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 ..但无论如何。 您还应该测试是否有空行。 后者通常是换行符结束的文本文件的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM