Making arrays from text files

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Making arrays from text files

 
0
  #1
Nov 26th, 2006
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.
Attached Thumbnails
screenshot.jpg  
Attached Files
File Type: zip Gradebook.zip (82.2 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Making arrays from text files

 
0
  #2
Nov 26th, 2006
Sorry, I don't see what you're exactly having problems with?

Do you need to create a class for your student grade problem?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Making arrays from text files

 
0
  #3
Nov 26th, 2006
I am having problems with creating a second structure/array that data will be read into from a text file. The data is originally written from a listbox if that matters doing a line by line write. The other problem I am having is searching a student's name and trying to get that data from the array just created using the text file. I hope I am answering your question to my question, if not, could you clarify, thank you.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Making arrays from text files

 
0
  #4
Nov 27th, 2006
Each line will be fed into a class structure:

  1. Student 999 999 999 999 X

You would split the line using the space as a delimiter. Look up the split method.

Now you would feed these variables into your class.

I.e
  1. class student
  2. {
  3. string name
  4. double scoreOne
  5. double scoreTwo
  6. double scoreThree
  7. double scoreFour
  8. double average
  9. }

To search for a name you would just look at the name attributes for all your data.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Making arrays from text files

 
0
  #5
Nov 28th, 2006
In my programming class, the instructor did discuss this project a little more, and made it sound like it would be easier to put one piece of data on per line, and then reading line by line, but when i do this, it says that the index is out of bounds, even though the first value is 0
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Making arrays from text files

 
0
  #6
Nov 29th, 2006
To explain my last post, it is the pointer of the structure array that is creating the error. The code that I have is listed below:

--------------------------------------------------------------------------------

count = 0
SearchStudents(count - 1).stuName = reader.ReadLine
Do While SearchStudents(count - 1).stuName <> "-1"
SearchStudents(count - 1).stuTest1 = CDbl(reader.ReadLine)
SearchStudents(count - 1).stuTest2 = CDbl(reader.ReadLine)
SearchStudents(count - 1).stuTest3 = CDbl(reader.ReadLine)
SearchStudents(count - 1).stuAvg = CDbl(reader.ReadLine)
SearchStudents(count - 1).stuGrade = CStr(reader.ReadLine)
count = count + 1
Loop
reader.Close()

---------------------------------------------------------
I have no idea why this is creating the error "IndexOutOfRangeException was unhandled", all I know is that it highlights the second line of code that I have given. The entire project is attached to this post so you could try to find what might really be throwing this error. Again thanks, and so you know, this attached file is newer than the first one that I attached earlier.
Attached Files
File Type: zip Project 9 - Gradebook option.zip (81.7 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Making arrays from text files

 
0
  #7
Nov 29th, 2006
Can you post just the code here. I don't like downloading zip files in case they contains viruses and other known nasties.:cheesy:

A few hints.

'SearchStudents(count - 1).stuName = reader.ReadLine
If that's where the problem is try taking that line out altogether.

SearchStudents(count - 1).
Why the (count - 1). try changing it to SearchStudents(count)
Last edited by iamthwee; Nov 29th, 2006 at 3:16 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Making arrays from text files

 
0
  #8
Nov 29th, 2006
Another very good tip would be to create a completely new project.

Then just try to read that file into the structure and display it. That way if there are any errors you know it is only to do with that section of code. Trying to do that within a big project is bad, because you don't know if something else is causing the problem.

Isolate and deal with it.
Last edited by iamthwee; Nov 29th, 2006 at 3:29 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Making arrays from text files

 
0
  #9
Nov 29th, 2006
Originally Posted by iamthwee View Post
Can you post just the code here. I don't like downloading zip files in case they contains viruses and other known nasties.:cheesy:
in this case, no. a zip file is better because it contains lots of files and some of them contain binary information. Besides, you should always be running a virus scanner which will catch viruses.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Making arrays from text files

 
0
  #10
Nov 29th, 2006
in this case, no. a zip file is better because it contains lots of files and some of them contain binary information. Besides, you should always be running a virus scanner which will catch viruses.
Wrong! All I need is the code and perhaps the text file he's using, then I can compile it from within my IDE. Have you ever used .net before. No it's not like MFC, thank god. :cheesy:

Also while I was away, I was thinking perhaps you're trying to read the whole file into your structure which is allocates space for only five lines.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC