I am having problems trying to create arrays from text files. The text files created from the program I am working on is like this:
Name Test1 Test2 Test3 Avg Grade
===================================
Student 999 999 999 999 X
The first two lines in this are headings that are added into the text file, and I need the student name, the test scores, avg and grade letter as seperate parts of a structure.
This is for a basic programming class, but the instructor hasn't really gotten into details about how to do this at all. What I have to do with this is search for the student name as you would do in a database to find a student's grade, and we are assuming that we do not know how many students there are in the file.
I am assuming that you would like to view the code I have to see where I am coming from. I have been googling arrays and using MSDN Libraries for help. To view the code, it is between the dashes:
---------------------------------------------------------------------------------
OptionExplicitOn
Imports System.Math
Imports System.IO
Imports System.IO.File
PublicClass Form1
Inherits System.Windows.Forms.Form
Structure StudentRec
Dim Name As String
Dim test1, test2, test3 As Double
Dim Avg As Double
End Structure
Dim AStudents() As StudentRec = {}
Friend max As Integer
Friend pointer As Short
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnDisplay.Hide() : btnAccept.Hide() : lstDisplay.Hide() : txtName.Hide() : txtScore1.Hide()
txtScore2.Hide() : txtScore3.Hide() : Label6.Hide() : Label2.Hide() : Label3.Hide() : Label4.Hide()
Label5.Hide() : lblStudentCount.Hide() : btnDisplay.Hide() : btnSave.Hide()
btnDisplay.Enabled = False : btnAccept.Enabled = False : lstDisplay.Enabled = False : txtName.Enabled = False
txtScore1.Enabled = False : txtScore2.Enabled = False : txtScore3.Enabled = False : Label6.Enabled = False
Label2.Enabled = False : Label3.Enabled = False : Label4.Enabled = False : Label5.Enabled = False
lblStudentCount.Enabled = False : btnDisplay.Enabled = False : btnSave.Enabled = False
btnDisplay.Enabled = False
btnAccept.Enabled = False
lblStudentCount.Text = 0
lstDisplay.Items.Add("Name Test 1 Test 2 Test 3 Average Grade")
lstDisplay.Items.Add("==============================================================================================")
End Sub
Private Sub txtName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Leave
If IsNumeric(txtName.Text) = True Then
txtName.Clear()
txtName.Focus()
MsgBox("I think you were trying to put a test score here, please put the student's name instead")
End If
If txtName.Text = "" Then
txtName.Focus()
End If
End Sub
Private Sub txtScore1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtScore1.Leave
If IsNumeric(txtScore1.Text) = False Then
MsgBox("You need to put the score for the first test")
txtScore1.Clear()
txtScore1.Focus()
Else
If CDbl(txtScore1.Text) > 100 Or CDbl(txtScore1.Text) < 0 Then
MsgBox("The test score is not correct")
txtScore1.Clear()
txtScore1.Focus()
End If
End If
End Sub
Private Sub txtScore2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtScore2.Leave
If IsNumeric(txtScore2.Text) = False Then
MsgBox("You need to put the score for the second test")
txtScore2.Clear()
txtScore2.Focus()
Else
If CDbl(txtScore2.Text) > 100 Or CDbl(txtScore2.Text) < 0 Then
MsgBox("The test score is not correct")
txtScore2.Clear()
txtScore2.Focus()
End If
End If
btnAccept.Enabled = True
End Sub
Private Sub txtScore3_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtScore3.Leave
If IsNumeric(txtScore3.Text) = False Then
MsgBox("You need to put the score for the third test")
txtScore3.Clear()
txtScore3.Focus()
Else
If CDbl(txtScore3.Text) > 100 Or CDbl(txtScore3.Text) < 0 Then
MsgBox("The test score is not correct")
txtScore3.Clear()
txtScore3.Focus()
End If
End If
End Sub
Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
lstDisplay.Items.Clear()
lstDisplay.Items.Add("Name Test 1 Test 2 Test 3 Average Grade")
lstDisplay.Items.Add("==============================================================================================")
pointer = 1 + UBound(AStudents)
lblStudentCount.Text = pointer + 1
ReDim Preserve AStudents(pointer)
AStudents(pointer).Name = CStr(txtName.Text)
AStudents(pointer).test1 = CDbl(txtScore1.Text)
AStudents(pointer).test2 = CDbl(txtScore2.Text)
AStudents(pointer).test3 = CDbl(txtScore3.Text)
txtName.Clear()
txtScore1.Clear()
txtScore2.Clear()
txtScore3.Clear()
txtName.Focus()
btnAccept.Enabled = False
btnDisplay.Enabled = True
End Sub
Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
lstDisplay.Items.Clear()
lstDisplay.Items.Clear()
lstDisplay.Items.Add("Name Test 1 Test 2 Test 3 Average Grade")
lstDisplay.Items.Add("==============================================================================================")
Dim avg, high, mid, low As Double
Dim Letter As String
For max = 0 To UBound(AStudents)
If AStudents(max).test1 > AStudents(max).test2 And AStudents(max).test1 > AStudents(max).test3 Then
high = AStudents(max).test1
If AStudents(max).test2 > AStudents(max).test3 Then
mid = AStudents(max).test2
low = AStudents(max).test3
Else
mid = AStudents(max).test3
low = AStudents(max).test2
End If
ElseIf AStudents(max).test2 > AStudents(max).test3 Then
high = AStudents(max).test2
If AStudents(max).test1 > AStudents(max).test3 Then
mid = AStudents(max).test1
low = AStudents(max).test3
Else
mid = AStudents(max).test3
low = AStudents(max).test1
End If
Else
high = AStudents(max).test3
If AStudents(max).test1 > AStudents(max).test2 Then
mid = AStudents(max).test1
low = AStudents(max).test2
Else
mid = AStudents(max).test2
low = AStudents(max).test1
End If
End If
avg = ((0.4 * high) + (0.35 * mid) + (0.25 * low))
If avg > 90 Then
Letter = "A"
ElseIf avg > 80 Then
Letter = "B"
ElseIf avg > 70 Then
Letter = "C"
ElseIf avg > 60 Then
Letter = "D"
Else
Letter = "F"
End If
lstDisplay.Items.Add(AStudents(max).Name.PadLeft(1) & AStudents(max).test1.ToString.PadLeft(25) & _
AStudents(max).test2.ToString.PadLeft(15) & AStudents(max).test3.ToString.PadLeft(20) & _
avg.ToString.PadLeft(20) & Letter.PadLeft(20))
Next
btnSave.Enabled = True
End Sub
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
Private Sub btnNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNew.Click
btnDisplay.Show() : btnAccept.Show() : lstDisplay.Show() : txtName.Show() : txtScore1.Show()
txtScore2.Show() : txtScore3.Show() : Label6.Show() : Label2.Show() : Label3.Show() : Label4.Show()
Label5.Show() : lblStudentCount.Show() : btnDisplay.Show() : btnSave.Show()
lstDisplay.Enabled = True : txtName.Enabled = True : txtScore1.Enabled = True
txtScore2.Enabled = True : txtScore3.Enabled = True : Label6.Enabled = True
Label2.Enabled = True : Label3.Enabled = True : Label4.Enabled = True : Label5.Enabled = True
lblStudentCount.Enabled = True : txtName.Focus()
End Sub
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
MsgBox("Saving will overwrite old file", MsgBoxStyle.OkOnly)
Dim writer As StreamWriter
writer = New StreamWriter("Grades.txt")
For max = 0 To lstDisplay.Items.Count - 1
writer.WriteLine(lstDisplay.Items.Item(max))
Next
writer.Close()
End Sub
Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
lstDocument.Items.Clear()
Dim reader As StreamReader
Try
reader = New StreamReader("Grades.txt")
While reader.EndOfStream = False
lstDocument.Items.Add(reader.ReadLine)
End While
reader.Close()
Catch ex As Exception
MsgBox("File does not exist, please create a new file first")
End Try
End Sub
Structure FindStudent
Dim stuName As String
Dim stuTest1, stuTest2, stuTest3, stuAvg As Double
Dim stuGrade As String
End Structure
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim stuSearch() As FindStudent = {}
Dim reader As StreamReader = File.OpenText("Grades.txt")
Dim loopcnt As Integer = 0
Dim foundname As String
Dim searchStu As Integer
stuSearch(loopcnt - 1).stuName = reader.ReadLine
Do While stuSearch(loopcnt - 1).stuName = reader.EndOfStream <> False
stuSearch(loopcnt - 1).stuTest1 = reader.ReadLine
stuSearch(loopcnt - 1).stuTest2 = reader.ReadLine
stuSearch(loopcnt - 1).stuTest3 = reader.ReadLine
stuSearch(loopcnt - 1).stuAvg = reader.ReadLine
stuSearch(loopcnt - 1).stuGrade = reader.ReadLine
loopcnt = loopcnt + 1
Loop
foundname = -1
If rdAll.Checked = True And txtNameLookup.Text <> "" Then
For searchStu = 0 To loopcnt
If txtNameLookup.Text = stuSearch(loopcnt).stuName Then
foundname = loopcnt
Exit For
End If
MsgBox("You Entered " & txtNameLookup.Text & " and the person found was " & stuSearch(foundname).stuName)
Next
ElseIf rdFinal.Checked = True And txtNameLookup.Text <> "" Then
For searchStu = 0 To loopcnt
If txtNameLookup.Text = stuSearch(loopcnt).stuName Then
foundname = loopcnt
Exit For
End If
MsgBox("You entered " & txtNameLookup.Text & " and their final grade was " & stuSearch(foundname).stuGrade)
Next
ElseIf rdGrades.Checked = True And txtNameLookup.Text <> "" Then
For searchStu = 0 To loopcnt
If txtNameLookup.Text = stuSearch(loopcnt).stuName Then
foundname = loopcnt
Exit For
End If
MsgBox("You entered " & txtNameLookup.Text & "and their grade letter was " & stuSearch(foundname).stuGrade)
Next
ElseIf rdPercent.Checked = True And txtNameLookup.Text <> "" Then
For searchStu = 0 To loopcnt
If txtName.Text = stuSearch(loopcnt).stuName Then
foundname = loopcnt
Exit For
End If
MsgBox("You entered " & txtName.Text & "and their percentage was " & stuSearch(foundname).stuAvg)
Next
Else
MsgBox("There is an error searching for the student, please try again")
txtName.Clear()
txtName.Focus()
End If
End Sub
EndClass
----------------------------------------------------------
If you have any ideas how to go about this, please let me know because I have been working on this for a very long time, try a week and a half now. To make matters worse, I have to do a similar thing for my linux class using the VIM editor, searching for an animal and then looking up what food it eats using sort, gep, etc without intermediate files and using pipes. I know this is a lot to ask for, but I am so new to this. Thank you in advance for your help.