首页 > 解决方案 > 将项目添加到数组 (VB 2008)

问题描述

该程序的目标是使用 StreamReader 解释文件中的曲棍球统计数据,然后显示添加的点列。下面的代码有点这样做,但是它没有将点值添加到数组的意义上是无效的 - 它单独输出它。寻求有关如何将点值合并到 aryTextFile() 中的帮助;

    Dim hockeyFile, LineOfText, aryTextFile() As String
    Dim i As Integer
    Dim nameText(), NumberText(), goalsText(), assistsText(), GamesWonText() As String
    Dim IntAssists(), IntGoals(), PointsText() As Single

    hockeyFile = "C:\Users\Bob\Downloads\hockey.txt" 'state location of file

    Dim objReader As New System.IO.StreamReader(hockeyFile) 'objReader can read hockeyFile


    For i = 0 To objReader.Peek() <> -1 'reads each line seperately, ends when there is no more data to read
        LineOfText = objReader.ReadLine 'stores seperate lines of data in HockeyFile into LineofText  
        aryTextFile = LineOfText.Split(",") 'takes lines and converts data into array
        Name = aryTextFile(0) 'first piece of data in lines of text is the name 
        nameText(i) = aryTextFile(0)
        If nameText(0) = "Name" Then
            TextBox1.Text = LineOfText & ", Points." & vbCrLf 'displays first line fo text and adds "Points" label
        End If

        If Name <> "Name" Then 'when second line starts, then begin to intepret data
            NumberText(i) = aryTextFile(1)
            assistsText(i) = aryTextFile(2) 'assists are in third value of array 
            goalsText(i) = aryTextFile(3) 'goals are in fourth value of array
            GamesWonText(i) = aryTextFile(4)

            IntAssists(i) = Val(assistsText(i)) 'since our assists value is a string by default, it must be converted to a integer

            IntGoals(i) = Val(goalsText(i)) 'since our goals value is a string by default, it must be converted to a integer

            PointsText(i) = (IntGoals(i) * 2) + (IntAssists(i)) 'goals are two points, assists are one point


            TextBox1.Text = TextBox1.Text & NumberText(i) & assistsText(i) & goalsText(i) & GamesWonText(i) & PointsText(i) & vbCrLf 'Displays points as last value in each line

        End If
    Next i

标签: arraysvb.net

解决方案


这应该让你非常接近:

它需要额外的验证。它没有考虑您在名称和目标之间的任何价值。

Private Sub ProcessHockeyStats()

    Try

        Dim inputFile As String = "c:\temp\hockey.txt"
        Dim outputFile As String = "c:\temp\output.txt"

        If Not File.Exists(inputFile) Then
            MessageBox.Show("Missing input file")
            Return
        End If

        If File.Exists(outputFile) Then
            File.Delete(outputFile)
        End If

        Dim lines() As String = File.ReadAllLines(inputFile)

        Dim output As List(Of String) = New List(Of String)

        Dim firstLine As Boolean = True

        For Each line As String In lines

            Dim values() As String = line.Split(","c)
            Dim points As Integer

            If firstLine Then
                output.Add("Name, Assists, Goals, Points")
                firstLine = False
            Else
                'needs validation for values
                points = CInt(values(1) * 2) + CInt(values(2))
                output.Add(String.Concat(line, ",", points))
            End If

        Next

        File.WriteAllLines("c:\temp\outfile.txt", output)

    Catch ex As Exception
        MessageBox.Show(String.Concat("Error occurred: ", ex.Message))
    End Try

End Sub

推荐阅读