Reading a Table from a file to an Array????????

  • Thread starter Thread starter ollie78
  • Start date Start date
O

ollie78

Guest
Hey I need help!!!!!!!!! We are supposed to do a specific program for my VB.NET class. And we have not learned how to do this. Can anyone help????
we have to read from a text file a 6 by 5 table into an array in VB.NET and then do calculations from this array.

Here is the assignment and the table:::

Open the data file in the form load event and close the data file
in the unload event. You can load the array into memory on the
form load or when click event of the Get Present Value button.
The annuity table

.980 .962 .943 .926 .909 .893
1.942 1.886 1.833 1.783 1.736 1.690
2.886 2.775 2.673 2.577 2.487 2.402
3.808 3.630 3.465 3.312 3.170 3.037
 
Heres clue...

Use the StreamReader to open a file and read its contents,

Then use the ReadLine method to read each line (while in a loop),

In the loop, use the Split to place the contents of each line into an array.

I really dont want to post any sample code as this is an assignment.

Post back if you are stuck.
 
Lets say you read a line...

this Split is based on each item having a space in between words
Code:
SomeLine = sr.ReadLine()
myArray(nCounter)  = SomeLine.Split(" "c)
nCounter +=1
 
I am really sorry.... but thank you so far for the help. Like I said we really have not learned any of this and it is not in our book so we are just rying to wing it.

And I would do all this in a loop contingent on the counter, or how many line-records? like for 4 lines it would be do until ncounter >4???
(" "c)....what is the c?
And how would that SomeLine and my array be defined?
Would the someline be defined in a structure
and would the array somewhere be dim myarray(5,4) as string or single
 
not now I am not at a computer that has VB.NET on it.
But basically I startred with something like this
Structure ArrayStruc
<VBFixedLength(25??)>ArrayLine as string
end structure

And then in the Button code
dim udtArray as ArrayStruc
dim Array(5,4) as single
fileopen(.....info....)
For intRecords = 1 to 4
For intLine = 1 to 5
Fileget(1, udtArray, intRecords)
Array(intLine) = udtArray.ArrayLine
next intLine
Next intRecords
and then the computing but I know how to do that
 
There are much better ways of doing this, for now heres a simple example.
Also, you need System.IO
Code:
member vars...
    Private arr(,) As String
    Private m_Count As Integer = 0


   Private Function OpenFile(ByVal strPath As String) As Boolean
        Dim sr As StreamReader
        sr = File.OpenText(strPath)

        Dim strItems As String
        loop through the text file
        While sr.Peek <> -1
            strItems = sr.ReadLine()
            addArray(strItems)
        End While
        sr.Close()
        Return True file was there...with no errors
    End Function

    Public Sub addArray(ByVal strItems As String)
        Dim sTemp() As String = strItems.Split(","c)
        ReDim Preserve arr(3, m_Count)
        Dim x As Integer

        For x = 0 To 3
            arr(x, m_Count) = sTemp(x)
        Next
        m_Count += 1
    End Sub
 
Thank you

Thank you for your help. I will have to try some of this. Thank you again.
 
Back
Top