• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 48
  • Last Modified:

How do i fill an array of integers from the contents of a file.

How do i  fill an array of integers from the contents of a file. (i have a debug file called  -scores.txt- that has all the exam grades in it ) and Loop through the array and count how many grades are between 0-50, 51-75, and 76-100. Then output the number of grades in each label above each listbox.

i also attached the file to this question
bob saget
bob saget
  • 3
  • 2
1 Solution
Fernando SotoRetiredCommented:
Hi Bob;

The file you posted seems to be a HTML document, can you post the actual file?

bob sagetAuthor Commented:
Fernando SotoRetiredCommented:
Hi Bob;

The sample code below should do what you are looking for.
Imports System.IO

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    '' Linq query to read the data from the file and parse them into there groups
    '' Group 1 = 0 - 50
    '' Group 2 = 51 - 75
    '' Group 3 = 76 - 100
    Dim parseData = (From valStr As String In File.ReadAllLines("C:\Working Directory\scores.txt")
                     Let valInt = GetRange(valStr)
                     Group By valInt Into Group).ToList()

    '' Place the values into the correct label control
    For Each g In parseData
        If g.valInt = 1 Then
            lbl0_50.Text = g.Group.Count.ToString()
        ElseIf g.valInt = 2 Then
            lbl51_75.Text = g.Group.Count.ToString()
            lbl76_100.Text = g.Group.Count.ToString()
        End If

End Sub

'' Helper function to determine what group a score is in.
Private Function GetRange(ByVal inp As String) As Integer
    Dim input As Integer = -99
    Integer.TryParse(inp, input)
    If input >= 0 AndAlso input <= 50 Then
        Return 1
    ElseIf input >= 51 AndAlso input <= 75 Then
        Return 2
        Return 3
    End If
End Function

Open in new window

bob sagetAuthor Commented:
you my friend are beast.!!
Fernando SotoRetiredCommented:
Not a problem Bob, glad to help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now