none
create program RRS feed

  • Question

  • A program that continuously accepts students’ data until eof and produces a report that lists all students whose grade point averages are below 2.0.

    Saturday, May 13, 2017 3:14 AM

All replies

  • Read in (assuming a text file) a file via TextFieldParser, (there is a simple example at the bottom of the link) loop thre the lines and populate a class which represents your data. Since you are mentioning a numeric a conversion is needed as all data read is of type string, use Integer or Double or Decimal TryParse function to properly convert the elements. Once the data is in a List(Of YourClass) you can query the List using methods such as Average to get averages as indicated, below 2.0.

    Try the above, if you get stuck show us a sample of your data in the file and what you have tried along with exactly what you are having issues with.

    Here is an example that demonstrates the above idea I have laid out above w/o showing a sample of average.

    ''' <summary>
    ''' Demonstrates how to use TextFieldParser where each row is delimited by a comma, 
    ''' each row has six elements, first three are headers, last three are integers of data.
    ''' 
    ''' * The assertion is bare-bones in that I do not check all invalid conditions
    ''' * My advice is not to have headers on each row, just data.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Form1
        Private ItemsList As New List(Of Item)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(
                IO.Path.Combine(Application.StartupPath, "TextFile1.txt"))
    
                Reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
                Reader.Delimiters = New String() {","}
    
                Dim currentRow As String()
                Dim ElementCount As Integer = 0
                Dim LineNumber As Long = 1
                Dim Col4 As Int32 = 0
                Dim Col5 As Int32 = 0
                Dim Col6 As Int32 = 0
    
                While Not Reader.EndOfData
                    Try
                        currentRow = Reader.ReadFields()
                        ElementCount = currentRow.Count
                        If ElementCount <> 6 Then
    
                            ItemsList.Add(
                                New Item With
                                {
                                    .Process = False,
                                    .BadData = String.Join(",", currentRow),
                                    .LineNumber = LineNumber
                                }
                            )
                        Else
                            If Int32.TryParse(currentRow(3), Col4) AndAlso
                               Int32.TryParse(currentRow(4), Col5) AndAlso
                               Int32.TryParse(currentRow(5), Col6) Then
    
                                ItemsList.Add(
                                    New Item With
                                    {
                                        .Column1 = Col4,
                                        .Column2 = Col5,
                                        .Column3 = Col6,
                                        .Process = True,
                                        .LineNumber = LineNumber
                                    }
                                )
    
                            End If
                        End If
    
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MessageBox.Show("Line " & ex.Message & " is invalid.  Skipping")
                    End Try
                    LineNumber += 1
    
                End While
            End Using
    
            If ItemsList.Count > 0 Then
    
                Dim BadData = (From this In ItemsList Where Not this.Process).ToList
                Dim GoodData = (From this In ItemsList Where this.Process).ToList
    
                If BadData.Count > 0 Then
                    Console.WriteLine("Bad data")
                    For Each Item In BadData
                        Console.WriteLine("Line: {0} Data: {1}", Item.LineNumber, Item.BadData)
                    Next
                End If
                If GoodData.Count > 0 Then
                    Console.WriteLine("Good data")
                    For Each Item In GoodData
                        Console.WriteLine("Line: {0} Data: {1}", Item.LineNumber, Item.ToString)
                    Next
                End If
            End If
        End Sub
    End Class
    Public Class Item
        Public Sub New()
        End Sub
        Public Property Process As Boolean
        Public Property Column1 As Int32
        Public Property Column2 As Int32
        Public Property Column3 As Int32
        Public Property BadData As String
        Public Property LineNumber As Long
        Public Overrides Function ToString() As String
            Return Column1.ToString & " " & Column2.ToString & " " & Column3.ToString
        End Function
    End Class
    

    Silly sample data in the text file

    Col1,Col2,Col3,1,2,3
    Col1,Col2,Col3,11,22,33
    Col1,Col2,Col3,1,3
    Col1,Col2,Col3,4,6,7
    Col1,Col3,11,22,33

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, May 13, 2017 3:45 AM
    Moderator