473,698 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.NET populate 2 dimensional array from a text file?

8 New Member
In Visual Basic.NET 2003, 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.

The instructor is gone for the week, so I can't ask her any more questions about this project now. It is due next week, and I haven't even gotten the first part of it figured out! Here's what the she said, when I asked her about this project.

"You do not need the inner FOR loop; since you know that there are only two columns, just write directly to those elements; e.g., strVenues(intRo w, 0) and strVenues(intRo w,1).

Also, you are not separating the data. You are filling the columns with the entire record. The fields in the record have to be separated and stored in their respective columns.


When you do your readline, you are reading the entire record into your variable strVen. The record has more than one field. You've got to use string manipulation to separate the fields and place them in the correct columns in the array.

Same thing with the list box; you only want to list the events and nothing else."

(I have not gotten to the list box part of this assignment yet.)

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 the problem? If so, how would I fix it?

I think this is how you put just the first field into the array. Can you tell me where the error(s) is/are in my code?

Expand|Select|Wrap|Line Numbers
  1.     Private Sub readFile() 'reads file into an array
  2.  
  3.         'declare variables
  4.  
  5.         Dim sreReader As IO.StreamReader
  6.  
  7.         'open the file
  8.         sreReader = IO.File.OpenText("venues.txt")
  9.  
  10.         'read each record into the array until the array is filled
  11.  
  12.         Dim strVen As String, intIndex, intCount As Integer
  13.  
  14.         For intCount = 0 To 10
  15.             strVen = sreReader.ReadLine()
  16.             strVenues(intCount, 0) = strVen.Substring(0, strVen.IndexOf(","))
  17.             intIndex = strVen.IndexOf(",") + 1 
  18.             strVenues(intCount, 1) = strVen.Substring(intIndex)
  19.  
  20.         Next intCount
  21.  
  22.         sreReader.Close()
  23.  

I have tried for hours and hours, 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
Jul 12 '07 #1
3 9135
OliveOyl3471
8 New Member
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Dim strVen As String, intIndex, intCount As Integer
  3.  
  4.         '  For intCount = 0 To 10
  5.  
  6.         ' strVen = sreReader.ReadLine()
  7.  
  8.         ' strVenues(intCount, 0) = strVen.Substring(0)
  9.  
  10.         'intIndex = strVen.IndexOf(",", intIndex) + 1
  11.  
  12.         'strVenues(intCount, 1) = strVen.Substring(0)
  13.  
  14.         'intIndex = strVen.IndexOf(",", intIndex) + 1
  15.  
  16.         ' Next intCount 
  17.  
  18.  
  19.  
  20.         'Do While sreReader.Peek() <> -1
  21.  
  22.         For intCount = 0 To 10
  23.  
  24.             strVen = sreReader.ReadLine()
  25.  
  26.             strVenues(intCount, 0) = strVen.Substring(0, strVen.IndexOf(","))
  27.  
  28.             intIndex = strVen.IndexOf(",")  
  29.  
  30.             strVenues(intCount, 1) = strVen.Substring(intIndex, 1)
  31.  
  32.             'intIndex = strVen.IndexOf(",", intIndex) + 1
  33.  
  34.             'intIndex = strVen.IndexOf(",") + 1
  35.  
  36.         Next intCount
  37.  
  38.  
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Try
  3.  
  4.             Dim intX As Integer
  5.  
  6.             For intX = 0 To 10
  7.  
  8.                 testLabel.Text = strVenues(intX, 0) & ControlChars.NewLine
  9.  
  10.                 'intX = intX + 1
  11.  
  12.             Next intX
  13.  
  14.         Catch ex As System.IndexOutOfRangeException
  15.  
  16.         End Try
  17.  
  18.         ' For intIndex = 0 To 10
  19.  
  20.         ' testLabel.Text = strVen.Substring(0, strVen.IndexOf(",")) & ControlChars.NewLine
  21.  
  22.         ' Next intIndex
  23.  
  24.  
Jul 12 '07 #2
OliveOyl3471
8 New Member
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:

Expand|Select|Wrap|Line Numbers
  1.         'populate array from file
  2.  
  3.         Dim strVen As String, intIndex, intCount As Integer
  4.         For intCount = 0 To 10
  5.             strVen = sreReader.ReadLine()
  6.             strVenues(intCount, 0) = strVen.Substring(0, strVen.IndexOf(","))
  7.             intIndex = strVen.IndexOf(",") + 1
  8.             strVenues(intCount, 1) = strVen.Substring(intIndex, 1)
  9.         Next intCount
  10.  
  11.         'close the file
  12.         sreReader.Close()
  13.  
Jul 12 '07 #3
kenobewan
4,871 Recognized Expert Specialist
Well done thanks for sharing the solution.
Jul 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2581
by: smurf | last post by:
Seems a simple question, but I can't find a simple answer: I have created a two dimensional array. I wish to send the data, a row at a time to a graph plotting routine which is expected a one dimension array. I could create a temporary one dimensional array and populate from the original array and then pass it to the routine, but this would seem to be long winded (read slow), and may well give me problems, as the previous values in the...
4
2776
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
1
1398
by: dibblm | last post by:
I have a text file being created and need the values from the array that gets created to populate my text file. I am new and not very good. Kinda borrowing bits and pieces from here and there, so if my code is a mess (Which it is ) don't be alarmed. Myfile is the array that gets populated and I need it's results returned to the text file . Below is the code... Dim oFile As System.IO.File
1
2265
by: GB | last post by:
Hello, I have a recursion function which generate 1-dimensional vector of integers so for 3 cycles I have, for example, following vectors: int vec1 = {1,3,2,4}; int vec2 = {2,5,6,2}; int vec3 = {3,3,2,8}; I need to populate 2 dimensional array of integers int st_list ,which is initially is empty, with the content of the vectors above.
8
11822
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious way to do this is a nested for-loop - but we all know O(n^2) is bad. So I am looking for something like ArrayList.ToArray(), or Matlabs A(:). C#
4
4534
by: daqmaneng | last post by:
does anyone have any suggestions for writing a two dimensional array of data to a file. Currently, I have an array of 5 columns, with 1000 elements per array. I know that I can use a for next loop to go through each data point in the array and use the streamwriter class for each individual data point and write each point individually to the file. I would like to simplify this by simply writing the entire array to file.
10
3985
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item in the list A i want to associate an undetermined number of items. The complication for me is the fact that the items to associate in list A are undetermined.
3
3329
GTXY20
by: GTXY20 | last post by:
Hi All, I have been able to create a successful application with Python but I am being asked to create in VB. I have a text file with the following data: 1,a 1,b 1,c 2,a
4
3064
by: ASPnewb1 | last post by:
Hello, I have a 2-dimensional array created from the following text file: data1=asdfawe data2=2 data3=223d data4=22
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.