如何从文本文件中求和数字

时间:2015-05-07 03:13:55

标签: arrays vb.net text

我正在尝试从文本文件中读取“销售”并将它们总结为在我的应用程序的框中显示总销售额。我是通过2-D阵列做到这一点的。我有不同的“部队”和他们的卖东西。所以我选择一个队伍输入销售金额,然后点击添加,写入文件。我如何阅读和总结所有部队的总销售额?

这是我到目前为止所做的:

 Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
    If salesTextBox.Text = String.Empty Then
        'display error message for invalid amount
        MessageBox.Show("Please enter a valid amount.", "No entry",
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        Dim outFile As IO.StreamWriter
        Dim troop As String = troopComboBox.SelectedItem.ToString
        Dim sales As Integer

        Integer.TryParse(salesTextBox.Text, sales)

        outFile = IO.File.AppendText("troopsales.txt")
        'write to file
        outFile.WriteLine(troop & ", " & sales)
        'close file
        outFile.Close()
    End If

    'reset focus & defaults
    salesTextBox.Focus()
    troopComboBox.SelectedIndex = 0
End Sub


Private Sub displayButton_Click(sender As Object, e As EventArgs) Handles displayButton.Click
    If IO.File.Exists("troopsales.txt") Then

        Dim inFile As IO.StreamReader
        Dim testRead As String
        Dim t105Sales, t216Sales, t256Sales, t316Sales, t400Sales, t456Sales, t566Sales, t688Sales As Integer
        Dim totalSales As Integer
        inFile = IO.File.OpenText("troopsales.txt")

        Do Until inFile.Peek = -1
            testRead = inFile.ReadLine
            If testRead.Contains("105") Then
                t105Sales += totalSales
            ElseIf testRead.Contains("216") Then
                t216Sales += totalSales
            ElseIf testRead.Contains("256") Then
                t256Sales += totalSales
            ElseIf testRead.Contains("316") Then
                t316Sales += totalSales
            ElseIf testRead.Contains("400") Then
                t400Sales += totalSales
            ElseIf testRead.Contains("456") Then
                t456Sales += totalSales
            ElseIf testRead.Contains("566") Then
                t566Sales += totalSales
            ElseIf testRead.Contains("688") Then
                t688Sales += totalSales
            End If
        Loop
        inFile.Close()

        totalLabel.Text = totalSales.ToString()
    Else
        MessageBox.Show("Source file Invalid.", "No entry",
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
End Sub

我无法弄清楚如何区分销售或阅读销售。

文本文件中的示例为:

105, 100 256, 300 105, 10 216, 20 316, 60 400, 120 456, 240 688, 160 256, 10 105, 30 400, 100 256, 200

左边的数字是部队号码,右边是销售额。点击显示时,我需要汇总并显示总销售额。

2 个答案:

答案 0 :(得分:3)

尝试这样做:

    Dim troopsales = _
        File _
            .ReadAllLines("D:\Users\James\Temporary\troopsales.txt") _
            .Select(Function(x) x.Split({","c, " "c}, StringSplitOptions.RemoveEmptyEntries)) _
            .GroupBy(Function(x) x(0), Function(x) Integer.Parse(x(1))) _
            .Select(Function(x) New With {.TroopNum = x.Key, .Sales = x.Sum()})

从您的示例数据中我得到了这个结果:

result

答案 1 :(得分:0)

所以,如果我理解正确的话,每一行的第一个数字是部队,其他所有人都是销售人员。基于此,您可以使用File.ReadAllLines静态方法将文本文件读入字符串数组。然后使用foreach循环迭代数组并发送  每个字符串到一个方法thal将spilt用逗号(,)并总结除第一个之外的所有数组元素。 这将为你提供每个部队的总和。您可以将此方法的结果与销售总额相加。