2016-10-17 1 views
0

テキストファイル内の値を見つけて同じ行の別の値を読み取るためのコードです。私が読んでいる大きなテキストファイルからわかっているのは、私が丁寧に書いている初期値の2つ以上の行が存在することです。これは部品番号です。部品番号を見つけた後、私は数量を取得しています。これを修正して、部品番号の行の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 

答えて

0

単一の数量を見つけて返すのではなく、数量の合計を維持するためにループを変更するだけで済みます。それぞれの変更は、以下のコードでコメントがありますが、基本的には文字列の代わりに数量の合計を返すように関数を変更しています。各行について、検索が見つかると、ループは数量値をそれが保持している合計に加算します。すべての行が処理された後、数量の合計が返されます。

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 
+0

おかげで..私は「qtySum + = Convert.ToDouble(ライン(9))エラー:タイプの未処理の例外 'に関するエラー取得していますsoohooniganにSystem.FormatException' のがmscorlib.dll で発生しました追加情報:入力文字列が正しい形式ではありませんでした。 – Noob2Java

+0

これは、Line(9)の値の1つが数値ではないことを意味します。具体的に何が原因でエラーが発生しているか調べるには、Try CatchブロックとCatchブロックのprint line(9)コードが変換しようとしているものを見ることができます...おそらく、その文字列に変換できないアルファベットがあるか、数字が全くありません。 – soohoonigan

+0

まさに私が必要でした。私は、値の位置がテキストファイルで変更されたことに気付きました。これは素晴らしいです。ありがとうsoohoonigan、あなたは素晴らしいです! – Noob2Java

関連する問題