Jump to content
Xtreme .Net Talk

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


Recommended Posts

Guest ollie78
Posted

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

  • Moderators
Posted

Here's 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 don't want to post any sample code as this is an assignment.

 

Post back if you are stuck.

Visit...Bassic Software
Guest ollie78
Posted

what is a split??

 

We got the whole open, read thing but what is a split???

Guest ollie78
Posted

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

Guest ollie78
Posted

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

  • Moderators
Posted

There are much better ways of doing this, for now here's a simple example.

Also, you need System.IO

'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

Visit...Bassic Software
Guest ollie78
Posted

Thank you

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...