【问题标题】:Find all instances from text file and return values从文本文件中查找所有实例并返回值
【发布时间】:2016-10-17 13:54:20
【问题描述】:

我有这段代码用于在文本文件中查找一个值,然后在同一行中读取另一个值。此后,我从正在阅读的大型文本文件中发现,可以有两行或多行带有我要搜索的初始值,即零件号。找到零件号后,我得到了数量。如何修改它以找到 ALL 的零件编号行,然后从该行返回每个值?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fp as string = "" 'enter the full path to your file here
    Dim value as string = GetValueForPart(fp, Me.TextBox1.Text)
    MsgBox(value) 'in this example, value is set to "6.237" when textbox input is "TR2999-01G"
End Sub

Private Function GetValueForPart(ByVal filepath As String, ByVal SearchPartNum As String) As String
    If Not File.Exists(filepath) Then Return Nothing
    If SearchPartNum Is Nothing OrElse SearchPartNum.Trim = "" Then Return Nothing
    Dim ret As String = Nothing
    Using sr As New StreamReader(filepath)
        Do While sr.Peek >= 0
            Dim line() As String = sr.ReadLine.Split(CChar("|"))
            If line IsNot Nothing AndAlso line.Count >= 5 Then
                If line(1).Equals(SearchPartNum) Then
                    ret = line(9)
                    Exit Do
                End If
            End If
        Loop
    End Using
    Return ret
End Function

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您只需更改循环以保持数量的运行总和,而不是只找到一个数量并返回它。每个修改在下面的代码中都有注释,基本上我们只是将函数更改为返回数量总和而不是字符串。对于每一行,如果找到搜索,则循环会将数量值添加到它保留的总和中。处理完所有行后,它将返回数量的总和。

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fp As String = ""
        'Dim value As String = GetValueForPart(fp, Me.TextBox1.Text)
        Dim value As Double = GetValueForPart(fp, Me.TextBox1.Text) 'need to change this to accept a double return
        MsgBox(value.ToString) 'add conversion to string just for display
    End Sub
    
    Private Function GetValueForPart(ByVal filepath As String, ByVal SearchPartNum As String) As Double 'change this to return double
        If Not File.Exists(filepath) Then Return Nothing
        If SearchPartNum Is Nothing OrElse SearchPartNum.Trim = "" Then Return Nothing
        'Dim ret As String = Nothing
        Dim qtySum As Double = 0 'change this to keep a running sum of the quantities returned
        Using sr As New StreamReader(filepath)
            Do While sr.Peek >= 0
                Dim line() As String = sr.ReadLine.Split(CChar("|"))
                If line IsNot Nothing AndAlso line.Count >= 5 Then
                    If line(1).Equals(SearchPartNum) Then
                        'ret = line(9)
                        'Exit Do
                        'Instead of finding a quantity and exiting, convert each quantity found
                        'into a double and add it to our running sum
                        qtySum += Convert.ToDouble(line(9))
                    End If
                End If
            Loop
        End Using
        'Return ret
        Return qtySum
    End Function
    

    【讨论】:

    • 谢谢soohoonigan .. 我在'qtySum += Convert.ToDouble(line(9)) 上收到错误错误:mscorlib.dll 中发生'System.FormatException' 类型的未处理异常附加信息:输入字符串的格式不正确。
    • 这意味着 line(9) 值之一不是数字,要找出导致该错误的具体原因,您可以将该行包装在 Try Catch 块中,并在 Catch 块打印行中(9) 的值,以便您可以看到代码试图转换的内容......很可能,该字符串中有某种字母字符无法转换,或者根本没有数字跨度>
    • 正是我需要的。我刚刚也意识到文本文件中值的位置已经改变。这很好用。非常感谢soohoonigan,你太棒了!
    猜你喜欢
    • 1970-01-01
    • 2011-05-02
    • 2017-01-30
    • 2013-11-24
    • 2013-12-12
    • 1970-01-01
    • 2011-08-05
    • 2023-01-12
    • 1970-01-01
    相关资源
    最近更新 更多