Help using stream reader to an array

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 11842 Views - Last Post: 14 December 2010 - 10:19 PM Rate Topic: -----

#1 mxrider   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-December 10

Help using stream reader to an array

Posted 14 December 2010 - 12:25 PM

I need to read a text file states.txt into an array and im having a little trouble. I need to write the lines into two arrays because the file goes

Abreviation
State Name
Abreviation
State Name
etc...




Im trying to do it the way my book does it but my book is rather confusing.

NameArray(ArrayVariable) = StateFiles.ReadLine()
            AbreviationArray(ArrayVariable) = StateFiles.ReadLine()



That code perticular doesnt want to read to an array?


Heres the rest of what i have.

Thanks!



Imports System.IO



Public Class Form1

    Dim ItemFoundBoolean As Boolean = False
    Dim ItemIndexInteger As Integer
    Dim ArrayVariable As Integer = 0
    Private StateFiles As StreamReader
    Dim NameArray() As ArrayList
    Dim AbreviationArray() As ArrayList


    Private Sub Form_Load(ByVal send As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        StateFiles = New StreamReader("states.txt")
        Do While StateFiles.Peek <> -1

            NameArray(ArrayVariable) = StateFiles.ReadLine()
            AbreviationArray(ArrayVariable) = StateFiles.ReadLine()

            
        Loop

        

    End Sub
    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        NameTextBox.Text = ""
        AbbreviationTextBox.Text = ""
        AbbreviationRadioButton.Checked = False
        StateRadioButton.Checked = False
    End Sub

    Private Sub LookUpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpButton.Click
        If AbbreviationRadioButton.Checked = True Then
            AbbreviationTextBox.Enabled = False
            NameTextBox.Enabled = True

        End If


        If StateRadioButton.Checked = True Then
            NameTextBox.Enabled = False
            AbbreviationTextBox.Enabled = True

        End If
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Help using stream reader to an array

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Help using stream reader to an array

Posted 14 December 2010 - 12:46 PM

You need to increment the array index. This is copying the file contents to array location zero only:

...
 Dim ArrayVariable As Integer = 0
...
 Do While StateFiles.Peek <> -1

            NameArray(ArrayVariable) = StateFiles.ReadLine()
            AbreviationArray(ArrayVariable) = StateFiles.ReadLine()

            
        Loop
...



ArrayVariable += 1

This post has been edited by n8wxs: 14 December 2010 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 sharpy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 396
  • Joined: 02-June 07

Re: Help using stream reader to an array

Posted 14 December 2010 - 12:59 PM

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        


        Try

            Dim sr As StreamReader
            Dim fs As FileStream
            fs = New FileStream(("C:\MFSProFilesACDB\TmeFiles\textfile.txt"), FileMode.OpenOrCreate)
            'stream reader object to read streamed input 
            sr = New StreamReader(fs)
            Dim itm As Object
            'reading line by line 
            itm = sr.ReadLine

            While Not itm = Nothing
                Dim split As String() = itm.Split(New [Char]() {","})
                TextBox1.Text = split(0)
                TextBox2.Text = split(1)
                TextBox3.Text = split(2)



                itm = sr.ReadLine
            End While
            sr.Close()
            fs.Close()
        Catch ex As System.Exception

            System.Windows.Forms.MessageBox.Show(ex.Message, "Load Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
      
    End Sub
End Class



Hi

Had a little play. I added 3 textboxes & a button to a form. The code paths to a textfile which is split by a delimiter which can be anything I used a comma.

My text file looks like this

Firstname,Lastname,Age


Hope this is what you are looking for

This post has been edited by sharpy: 14 December 2010 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Help using stream reader to an array

Posted 14 December 2010 - 01:01 PM

To add something to an ArrayList you use the .Add method.

StateFiles = New StreamReader("states.txt")
   Do While StateFiles.Peek <> -1
      NameArray.Add(StateFiles.ReadLine())
      AbreviationArray.Add(StateFiles.ReadLine())
   Loop

Was This Post Helpful? 1
  • +
  • -

#5 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Help using stream reader to an array

Posted 14 December 2010 - 01:22 PM

View Posthawkvalley1, on 14 December 2010 - 12:01 PM, said:

To add something to an ArrayList you use the .Add method.

StateFiles = New StreamReader("states.txt")
   Do While StateFiles.Peek <> -1
      NameArray.Add(StateFiles.ReadLine())
      AbreviationArray.Add(StateFiles.ReadLine())
   Loop

Good catch! I just saw the array parens and didn't look at the type.
Was This Post Helpful? 0
  • +
  • -

#6 mxrider   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-December 10

Re: Help using stream reader to an array

Posted 14 December 2010 - 01:26 PM

okay ive taken code from all of you and ive almost got it to work but i have errors on two lines which ive highlighed, any ideas?

'Todd Stolz-Schroeder
'Exam 2

Imports System.IO



Public Class Form1

    Dim ItemFoundBoolean As Boolean = False
    Dim ItemIndexInteger As Integer
    Dim ArrayVariable As Integer = 0
    Dim StateArrayVariable As Integer = 0
    Private StateFiles As StreamReader
    Dim NameArray() As ArrayList
    Dim AbreviationArray() As ArrayList


    Private Sub Form_Load(ByVal send As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Do While StateFiles.Peek <> -1


            NameArray(ArrayVariable).Add(StateFiles.ReadLine())
            AbreviationArray(ArrayVariable).Add(StateFiles.ReadLine())
            ArrayVariable += 1

        Loop



    End Sub
    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        NameTextBox.Text = ""
        AbbreviationTextBox.Text = ""
        AbbreviationRadioButton.Checked = False
        StateRadioButton.Checked = False
    End Sub

    Private Sub LookUpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpButton.Click
        If AbbreviationRadioButton.Checked = True Then
            AbbreviationTextBox.Enabled = False
            NameTextBox.Enabled = True

            Do Until ItemFoundBoolean = True
                "If NameTextBox.Text = NameArray(StateArrayVariable) Then"
                    ItemFoundBoolean = True
                    AbbreviationTextBox.Text = NameArray(StateArrayVariable).ToString
                Else
                    StateArrayVariable += 1
                End If
            Loop
        End If


        If StateRadioButton.Checked = True Then
            NameTextBox.Enabled = False
            AbbreviationTextBox.Enabled = True



            Do Until ItemFoundBoolean = True

                "If AbbreviationTextBox.Text = AbreviationArray(StateArrayVariable) Then"
                    ItemFoundBoolean = True
                    NameTextBox.Text = AbreviationArray(StateArrayVariable).ToString
                Else
                    StateArrayVariable += 1
                End If
            Loop
        End If
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#7 mxrider   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-December 10

Re: Help using stream reader to an array

Posted 14 December 2010 - 01:46 PM

Is there a way to upload my whole program so it would be easier to diagnose?
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Help using stream reader to an array

Posted 14 December 2010 - 02:00 PM

My advice is wrong. I didn't notice you were declaring the arrays as type ArrayList. You do not want to do that. :) Use ArrayLists directly as showed you.

Change your declarations:

'Todd Stolz-Schroeder
'Exam 2

Imports System.IO

Public Class Form1

    Dim ItemFoundBoolean As Boolean = False
    Dim ItemIndexInteger As Integer
'''''''    Dim ArrayVariable As Integer = 0
    Dim StateArrayVariable As Integer = 0

''' This declares a StreamReader but does not associate a file
    Private StateFiles As StreamReader

''' drop the array parenthesis
    Dim NameArray As ArrayList
    Dim AbreviationArray As ArrayList
'''

    Private Sub Form_Load(ByVal send As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

''' hawkvalley1's code changed to match file layout
        StateFiles = New StreamReader("states.txt")
        Do While StateFiles.Peek <> -1
           AbreviationArray.Add(StateFiles.ReadLine())
           NameArray.Add(StateFiles.ReadLine())
        Loop

    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        NameTextBox.Text = ""
        AbbreviationTextBox.Text = ""
        AbbreviationRadioButton.Checked = False
        StateRadioButton.Checked = False
    End Sub

    Private Sub LookUpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpButton.Click
        If AbbreviationRadioButton.Checked = True Then
            AbbreviationTextBox.Enabled = False
            NameTextBox.Enabled = True

            Do Until ItemFoundBoolean = True
                "If NameTextBox.Text = NameArray(StateArrayVariable) Then"
                    ItemFoundBoolean = True
                    AbbreviationTextBox.Text = NameArray(StateArrayVariable).ToString
                Else
                    StateArrayVariable += 1
                End If
            Loop
        End If


        If StateRadioButton.Checked = True Then
            NameTextBox.Enabled = False
            AbbreviationTextBox.Enabled = True



            Do Until ItemFoundBoolean = True

                "If AbbreviationTextBox.Text = AbreviationArray(StateArrayVariable) Then"
                    ItemFoundBoolean = True
                    NameTextBox.Text = AbreviationArray(StateArrayVariable).ToString
                Else
                    StateArrayVariable += 1
                End If
            Loop
        End If
    End Sub
End Class



This post has been edited by n8wxs: 14 December 2010 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#9 roadfury   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 05-February 07

Re: Help using stream reader to an array

Posted 14 December 2010 - 02:01 PM

Are you supposed to be using an Array, or an ArrayList? Your book example looks like it's using an Array, but in your code you're using an ArrayList. The functions in your code seem to be combining elements of the two, which is probably causing the issues.

Let us know if you're using Array, or ArrayLists, so we're formatting our answers correctly.
Was This Post Helpful? 0
  • +
  • -

#10 mxrider   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-December 10

Re: Help using stream reader to an array

Posted 14 December 2010 - 02:08 PM

im going to probably sound stupid but i dont know what the difference is, i was just trying a different codes to get it to work.


so i guess just use a regular array.

when i changed the arraylist to an array at the top i get an error where its highlighted and the bottom is fine now. i guess i have arraylist and array code mixed up.


Public Class Form1

    Dim ItemFoundBoolean As Boolean = False
    Dim ItemIndexInteger As Integer
    Dim ArrayVariable As Integer = 0
    Dim StateArrayVariable As Integer = 0
    Private StateFiles As StreamReader
    Dim NameArray() As Array
    Dim AbreviationArray() As Array


    Private Sub Form_Load(ByVal send As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Do While StateFiles.Peek <> -1


            "NameArray(ArrayVariable).Add(StateFiles.ReadLine())"
           " AbreviationArray(ArrayVariable).Add(StateFiles.ReadLine())"
            ArrayVariable += 1

        Loop



    End Sub
    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        NameTextBox.Text = ""
        AbbreviationTextBox.Text = ""
        AbbreviationRadioButton.Checked = False
        StateRadioButton.Checked = False
    End Sub

    Private Sub LookUpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpButton.Click
        If AbbreviationRadioButton.Checked = True Then
            AbbreviationTextBox.Enabled = False
            NameTextBox.Enabled = True

            Do Until ItemFoundBoolean = True
                If NameTextBox.Text = NameArray(StateArrayVariable).ToString Then
                    ItemFoundBoolean = True
                    AbbreviationTextBox.Text = NameArray(StateArrayVariable).ToString
                Else
                    StateArrayVariable += 1
                End If
            Loop
                End If


                If StateRadioButton.Checked = True Then
                    NameTextBox.Enabled = False
                    AbbreviationTextBox.Enabled = True



            Do Until ItemFoundBoolean = True
                If AbbreviationTextBox.Text = AbreviationArray(StateArrayVariable).ToString Then
                    ItemFoundBoolean = True
                    NameTextBox.Text = AbreviationArray(StateArrayVariable).ToString
                Else
                    StateArrayVariable += 1
                End If
            Loop
                End If
    End Sub
End Class


This post has been edited by mxrider: 14 December 2010 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

#11 roadfury   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 05-February 07

Re: Help using stream reader to an array

Posted 14 December 2010 - 02:54 PM

ArrayList is a collection object, whereas Array is just an array. ArrayList has several methods and properties that make working with arrays a lot easier, depending on the task.

To make the code use Arrays, make the following changes:
Dim NameArray() As String '() designates the variable as an Array. You need to tell it what kind of data will be in the array, in this case String.
Dim AbreviationArray() as String

...

Private Sub Form_Load(ByVal send As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	Do While StateFiles.Peek <> -1
		'Re-dimension the array to hold 1 more entry. Preserve prevents the existing data from being erased.
		Redim Preserve NameArray(ArrayVariable + 1)
		Redim Preserve AbreviationArray(ArrayVariable + 1)

		NameArray(ArrayVariable).Add(StateFiles.ReadLine())
		AbreviationArray(ArrayVariable).Add(StateFiles.ReadLine())
		ArrayVariable += 1
	Loop
...



Additionally, you have created StateArrayvariable as a global variable. In your LookUpButton_Click function, add: StateArrayVariable = 0 near the top of the function. Otherwise the second time you click the button, the loop will start with StateArrayVariable at the last number. This will most likely cause an Out Of Bounds error.
Was This Post Helpful? 0
  • +
  • -

#12 mxrider   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-December 10

Re: Help using stream reader to an array

Posted 14 December 2010 - 03:02 PM

I still have a few errors on these lines when i changed all that.


NameArray(ArrayVariable).Add(StateFiles.ReadLine())
AbreviationArray(ArrayVariable).Add(StateFiles.ReadLine())

heres the whole code to see what i changed.


Option Strict On
Imports System.IO

Public Class Form1

    Dim ItemFoundBoolean As Boolean = False
    Dim ItemIndexInteger As Integer
    Dim ArrayVariable As Integer = 0
    Dim StateArrayVariable As Integer = 0
    Private StateFiles As StreamReader
    Dim NameArray() As Array
    Dim AbreviationArray() As Array


    Private Sub Form_Load(ByVal send As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Do While StateFiles.Peek <> -1

            ReDim Preserve NameArray(ArrayVariable + 1)
            ReDim Preserve AbreviationArray(ArrayVariable + 1)



            NameArray(ArrayVariable).Add(StateFiles.ReadLine())
            AbreviationArray(ArrayVariable).Add(StateFiles.ReadLine())

            ArrayVariable += 1

        Loop



    End Sub
    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        NameTextBox.Text = ""
        AbbreviationTextBox.Text = ""
        AbbreviationRadioButton.Checked = False
        StateRadioButton.Checked = False
    End Sub

    Private Sub LookUpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpButton.Click
        StateArrayVariable = 0


        If AbbreviationRadioButton.Checked = True Then
            AbbreviationTextBox.Enabled = False
            NameTextBox.Enabled = True

            Do Until ItemFoundBoolean = True
                If NameTextBox.Text = NameArray(StateArrayVariable).ToString Then
                    ItemFoundBoolean = True
                    AbbreviationTextBox.Text = NameArray(StateArrayVariable).ToString
                Else
                    StateArrayVariable += 1
                End If
            Loop
        End If


        If StateRadioButton.Checked = True Then
            NameTextBox.Enabled = False
            AbbreviationTextBox.Enabled = True



            Do Until ItemFoundBoolean = True
                If AbbreviationTextBox.Text = AbreviationArray(StateArrayVariable).ToString Then
                    ItemFoundBoolean = True
                    NameTextBox.Text = AbreviationArray(StateArrayVariable).ToString
                Else
                    StateArrayVariable += 1
                End If
            Loop
        End If
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#13 roadfury   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 05-February 07

Re: Help using stream reader to an array

Posted 14 December 2010 - 03:08 PM

Ooops, I forgot two changes:
    NameArray(ArrayVariable) = StateFiles.ReadLine()
    AbreviationArray(ArrayVariable) = StateFiles.ReadLine()



This part still had ArrayList code in it (.Add).
Was This Post Helpful? 0
  • +
  • -

#14 mxrider   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-December 10

Re: Help using stream reader to an array

Posted 14 December 2010 - 04:57 PM

View Postroadfury, on 14 December 2010 - 02:08 PM, said:

Ooops, I forgot two changes:
    NameArray(ArrayVariable) = StateFiles.ReadLine()
    AbreviationArray(ArrayVariable) = StateFiles.ReadLine()



This part still had ArrayList code in it (.Add).

the

StateFiles.ReadLine()

part of both of those lines still gives me an error.
Was This Post Helpful? 0
  • +
  • -

#15 roadfury   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 05-February 07

Re: Help using stream reader to an array

Posted 14 December 2010 - 05:21 PM

You left something out of your Form_Load sub... Can you find it?

Here's the answer:
Spoiler

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2