Arrays and text file

jeneal

Member
Joined
Nov 22, 2007
Messages
8
Programming Experience
Beginner
I need help populating a set of parallel arrays from a text file document. The file contaings a zipcode and a city seperated by a comma ("44202,Aurora"). I need them to be in parallel array so they can be searched and the coresponding value returned in a text box. How can I do this? I have been through many example codes, and many from my book, but I am unable to find a functional way to do this. Please help.
 
so you need to split that string by the coma

then store the first element to the first array and the second to the second array - right?

VB.NET:
Dim myString As String = "44202,Aurora"
        'creating an array to keep the split
        Dim MyInfo() As String = myString.Split(",")

        For Each s As String In MyInfo
            'do something
            MessageBox.Show(s)
        Next

the above example is for one element - all you need to do is to loop through your text document and perform a task similiar to the above!

Well.. if u need to put the individual elements in 2 different arraylists - you should declare a global arraylist and add each item after each line!

HTH
 
So this is what I have thus far, it seems logically correct, but its not functionable, can someone please look at and possibly help correct?

Thank you in advance!

VB.NET:
Dim readFile As String = IO.File.ReadAllText("C:\SAZC.txt")

        Dim userInput As String
        userInput = TextBox2.Text

        Dim myString As String = readFile
        'creating an array to keep the split
        Dim MyInfo() As String
        Dim separator As Char = ","
        Dim row As Integer
        Dim row2 As Integer
        Dim outbox As String

        MyInfo = readFile.Split(separator)

        Dim cityInfo(,) As String

        For Each s As String In cityInfo
            s = cityInfo
        Next s
        
        Do Until row = cityInfo.Length
            orelse userinput(row,0)
            row = row + 1
        Loop

        If row < cityInfo.Length Then
            outbox = userInput(row, 1)
        End If

        Do Until row2 = cityInfo.Length
            orelse userinput(0,row2)
            row2 = row2 + 1
        Loop

        If row < cityInfo.Length Then
            outbox = userInput(1, row2)
        End If

        TextBox1.Text = outbox
 
Last edited by a moderator:
Dim readFile As String = IO.File.ReadAllText("C:\Documents and Settings\Junell\My Documents\School\fall07\VB\Projects\program9\SAZC.txt")

why not read line by line? and seperate the string on each line?

considering that the textfile is in the "right format" like

("44202,Aurora")
("4455,abc")
("002,defg")
("007,hijk")
 
why not read line by line? and seperate the string on each line?

considering that the textfile is in the "right format" like

("44202,Aurora")
("4455,abc")
("002,defg")
("007,hijk")

How do I do that? When I try I get compiler errors
 
hmm there are several methods to read a file line by line using a StreamReader.

The following is a simple example

VB.NET:
        Dim sr As IO.StreamReader
        sr = My.Computer.FileSystem.OpenTextFileReader("C:\textFile.txt")

        'read from the file and display the data line by line
        Dim strString As String

        Do While sr.Peek <> -1
            'storing the line inside a variable
            strString = sr.ReadLine
            '@@@@@@@@@@@@@@
            'parse here@@@@@@@@@
            '@@@@@@@@@@@@@@
            'display inside the listbox
            ListBox1.Items.Add(strString)
            '
            Dim strNewLine As String = "New Line"
            ListBox1.Items.Add(strNewLine)
        Loop
        sr.Close()

the text file contains :

("44202,Aurora")
("4455,abc")
("002,defg")
("007,hijk")

the output is something like:

("44202,Aurora")
New Line
("4455,abc")
New Line
("002,defg")
New Line
("007,hijk")
New Line


HTH
 
Back
Top