3 Replies - 12778 Views - Last Post: 12 July 2007 - 03:06 PM Rate Topic: -----

#1 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

How do you populate a 2d array from a text file?

Posted 11 July 2007 - 11:15 PM

How do you populate a two dimensional array from a text file? I know how to check file existence, open the file for read, etc. I just can't get it to read more than one record from the file into the array.

I have declared a 2 dimensional, 12 row, 2 column array named strVenues(11,1) in the public section of the form.


I did get it to add only the first field in the record, but I cannot figure out how to get it to add all of the records from the file into the array! There are 11 records in the file, "venues.txt":

Toby Keith,C
Chicago,C
Nora Jones,C
Styx,C
Peter Pan,T
Mama Mia,T
Rent,T
Cats,T
David Copperfield,E
Paula Dean Cooking,E
Tractor Pull,E

I don't know why the instructions say to create a 12 row array, when there are 11 records. Could that be part of the problem?

I think this is how you put just the first field into the array, as I am required to do. (so why do I need a 2d array if we only add the first field?) I'm so confused!

I also have to populate a list box with just the first field, but I don't know if the information is supposed to come from the array or from the text file.

Why do I need an array if I can just read the text file into the list box?


Can you tell me where the error(s) is/are in my code?


	Private Sub readFile() 'reads file into an array

		'declare variables

		Dim sreReader As IO.StreamReader

		'open the file
		sreReader = IO.File.OpenText("venues.txt")

		'read each record into the array until the array is filled

		Dim strVen As String, intIndex, intCount As Integer

		For intCount = 0 To 10
			strVen = sreReader.ReadLine()
			strVenues(intCount, 0) = strVen.Substring(0, strVen.IndexOf(","))
			intIndex = strVen.IndexOf(",") + 1 
			strVenues(intCount, 1) = strVen.Substring(intIndex)

		Next intCount

		sreReader.Close()




I have tried for hours and hours, for several days, to figure this out! I watched the class lectures, studied the book and searched online, and tried different things with the code, but nothing works!!! Please help me figure this out!

Could it have something to do with the way I have used the intIndex and substring methods?

Any clue as to what I'm doing wrong will be most appreciated!

Thanks

p.s. this is Visual Basic.NET 2003

This post has been edited by OliveOyl3471: 11 July 2007 - 11:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do you populate a 2d array from a text file?

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: How do you populate a 2d array from a text file?

Posted 12 July 2007 - 12:13 PM

I have asked at three different forums (plus this one) and so far I haven't seen any response.

This is driving me nuts! I know it can be done, because my instructor knows what she's talking about, and she said to do it.

Here are a few of the many things I've been trying. The apostrophes mean that I have been experimenting with these things…erase the apostrophe and test it, add an apostrophe and test it, in different combinations:


		Dim strVen As String, intIndex, intCount As Integer

		'  For intCount = 0 To 10

		' strVen = sreReader.ReadLine()

		' strVenues(intCount, 0) = strVen.Substring(0)

		'intIndex = strVen.IndexOf(",", intIndex) + 1

		'strVenues(intCount, 1) = strVen.Substring(0)

		'intIndex = strVen.IndexOf(",", intIndex) + 1

		' Next intCount 



		'Do While sreReader.Peek() <> -1

		For intCount = 0 To 10

			strVen = sreReader.ReadLine()

			strVenues(intCount, 0) = strVen.Substring(0, strVen.IndexOf(","))

			intIndex = strVen.IndexOf(",")  

			strVenues(intCount, 1) = strVen.Substring(intIndex, 1)

			'intIndex = strVen.IndexOf(",", intIndex) + 1

			'intIndex = strVen.IndexOf(",") + 1

		Next intCount




Here's my test label, which still only displays the last record in the text file.

I think this might mean that the compiler IS adding all the records to the array, but erases the previous one as it adds another, leaving only the last record. But HOW would I fix it?



		Try

			Dim intX As Integer

			For intX = 0 To 10

				testLabel.Text = strVenues(intX, 0) & ControlChars.NewLine

				'intX = intX + 1

			Next intX

		Catch ex As System.IndexOutOfRangeException

		End Try

		' For intIndex = 0 To 10

		' testLabel.Text = strVen.Substring(0, strVen.IndexOf(",")) & ControlChars.NewLine

		' Next intIndex



Was This Post Helpful? 0
  • +
  • -

#3 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: How do you populate a 2d array from a text file?

Posted 12 July 2007 - 02:33 PM

I managed to discover the answer to this question on my own. In case anyone who reads this needs to know the answer, here it is:

I was not displaying the label correctly, for one thing.

Here is the code that will populate my 2d array from text file:

()
		'populate array from file

		Dim strVen As String, intIndex, intCount As Integer
		For intCount = 0 To 10
			strVen = sreReader.ReadLine()
			strVenues(intCount, 0) = strVen.Substring(0, strVen.IndexOf(","))
			intIndex = strVen.IndexOf(",") + 1
			strVenues(intCount, 1) = strVen.Substring(intIndex, 1)
		Next intCount

		'close the file
		sreReader.Close()


Was This Post Helpful? 0
  • +
  • -

#4 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: How do you populate a 2d array from a text file?

Posted 12 July 2007 - 03:06 PM

Well, you can use split:
dim MyStringToSplit as string = "Name,Sign"
dim SplitedArray() as string = split(MyStringToSplit,",")


note that now:
SplitedArray(0)="Name"
SplitedArray(1)="Sign"

But this is not the thing I would be consurned about if i was you, beacuse adding 1 dimentional arries to 2 dimentional 1 would use alot of memory management if the list is big. So you should know dimentions before.

dim filecontents as string = my.computer.filesystem.readalltext("venues.txt") 
dim PartiallySplitedArray() as string = split(MyStringToSplit,chr(13))
dim SplitedArray(1)(PartiallySplitedArray.getupperbound(0)) as string
dim buf() as string
dim i as integer = 0

for each element as string in PartiallySplitedArray
	buf= split(element,",")
	SplitedArray(0)(i)=buf
	SplitedArray(1)(i)=buf
	i+=1
next element



Anayhow you could do this without using buf and element, but i tought this would be more simple to understand.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1