0

College student in an advanced VB class who is turning to a forum for help - I've found a few examples of code but am having a hard time figuring this one out.. any and all help is appreciated :)

This application imports a .txt file stored in the bin, debug folder called data.txt ..20 records, 3 lines per record, the last line is the student's grade, I need to average the grades by summing each records grade and dividing by 20 and then displaying on the lstbox showing the average.

So far I've got..

    Dim objReader As IO.StreamReader
    Dim intFill As Integer
    Dim intCount As Integer = 0
    Dim intAverage As Integer
    Dim strLocationAndNameOfFile As String = "data.text"

    If IO.File.Exists(strLocationAndNameOfFile) = True Then
        objReader = IO.File.OpenText(strLocationAndNameOfFile)
    Else
        MsgBox("The file is not available. Restart the program when the file is avilable", , "Error")
        Me.Close()
    End If

    If IO.File.Exists(strLocationAndNameOfFile) Then
        objReader = IO.File.OpenText(strLocationAndNameOfFile)
        Do While objReader.Peek <> -1
            _strName(intCount) = Convert.ToString(objReader.ReadLine())
            _strItemID(intCount) = Convert.ToString(objReader.ReadLine())
            _intGrade(intCount) = Convert.ToInt32(objReader.ReadLine())
            intCount += 1
        Loop
        objReader.Close()
    End If

    For intFill = 0 To (_strName.Length - 1)
        *'intAverage = SUM OF ALL AVERAGES / LENGTH OF ARRAY -1*
        Me.lstAverage.Items.Add(intAverage.ToString())
  • I'm not sure I can see `Dim strLocationAndNameOfFile As String = "data.text"` working.. – christopher Mar 08 '13 at 21:31
  • It looks like your college taught you to use type prefixes. You should have a chat with those professors... this used to be important, but is **no longer recommended**. See Microsoft's own coding style guidelines, especially [this section](http://msdn.microsoft.com/en-us/library/ms229045.aspx) where they advise (in bold type no less) "Do not use hungarian notation". – Joel Coehoorn Mar 08 '13 at 21:50
  • Also, avoid `File.Exists()` http://stackoverflow.com/q/673654/3043 and since this is an advanced class, you should know to close your `objReader` object in a Finally block (possibly via a Using block). – Joel Coehoorn Mar 08 '13 at 21:58

2 Answers2

1

While looping to read the grades sum them up

    Dim total as Integer
    Do While objReader.Peek <> -1
        _strName(intCount) = Convert.ToString(objReader.ReadLine())
        _strItemID(intCount) = Convert.ToString(objReader.ReadLine())
        _intGrade(intCount) = Convert.ToInt32(objReader.ReadLine())
        total += _intGrade(intCount)
        intCount += 1
    Loop

And then just divide by 20 or _intGrade.Length

    intAverage = total / _intGrade.Length
Steve
  • 213,761
  • 22
  • 232
  • 286
1

So many issues, as much as I loathe doing other's homework I want you to see what this could look like

Public Function GetAverageGrade(ByVal filename As String) As Double
    Dim totalGrade As Integer = 0
    Dim lineCount As Integer = 0
    Dim line As String

    Using rdr As New IO.StreamReader(filename)
       While (line = rdr.ReadLine()) IsNot Nothing
           lineCount += 1
           If lineCount Mod 3 = 0 Then totalGrade += Convert.ToInt32(line)
       End While
    End Using
    Return totalGrade / (lineCount / 3.0)
End Function

Of course, you probably want to do more with that data than just get the average grade. So an even better option is build code that reads it all in as a set of records:

Public Class GradeItem
    Public Property Name As String
    Public Property Item As String
    Public Property Grade As Integer
End Class

and then

Public Iterator Function ReadGradeItems(ByVal fileName As String) As IEnumerable(Of GradeItem)
    Using rdr As New IO.StreamReader(fileName)
        While rdr.Peek() <> -1
            Yield New GradeItem With {.Name = rdr.ReadLine(), .Item= rdr.ReadLine(), .Grade = Convert.ToInt32(rdr.ReadLine()}
        End While
    End Using
End Function

and now put it all together:

Dim grades As IEnumerable(Of GradeItem) = ReadGradeItems("data.text")

lstAverage.Items.Add(grades.Select(Function(g) g.Grade).Average())
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794