10 Replies - 717 Views - Last Post: 29 March 2011 - 08:07 PM Rate Topic: -----

#1 Helpwithcode1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-February 11

Simple Error, cannot understand

Posted 29 March 2011 - 06:30 PM

Hello, I am trying to list the first 20 U.S. Presidents from an external file into an array and into a listbox, and then displays in another list box their first name. I keep getting an error at the "Presidents(i) = reader.ReadLine"


Private Sub btnpush_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpush.Click
        Dim Presidents(20) As Integer
        Dim reader As IO.StreamReader
        reader = IO.File.OpenText("P:\Presidents.txt")
        For i = 0 To 20
            Presidents(i) = reader.ReadLine
        Next i
        For i = 0 To 20
            lstout.Items.Add(Presidents(i))
        Next
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Simple Error, cannot understand

#2 n8wxs  Icon User is offline

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

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

Re: Simple Error, cannot understand

Posted 29 March 2011 - 06:32 PM

I assume you are reading strings from the file. Why are you trying to store them in integer array??

Dim Presidents(20) As Integer

Was This Post Helpful? 1
  • +
  • -

#3 Helpwithcode1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-February 11

Re: Simple Error, cannot understand

Posted 29 March 2011 - 06:33 PM

Already answered my own question :). It was because they were declared as integers and not as strings! Simple mistake, maybe I should do some more debugging before posting! :)
Was This Post Helpful? 0
  • +
  • -

#4 Helpwithcode1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-February 11

Re: Simple Error, cannot understand

Posted 29 March 2011 - 07:00 PM

Sorry another question, Since I printed out all of the names in one list box. I created a second listbox to display only their first names, but not sure how to print only their first names from the external file?
Public Class Form1

    Private Sub btnpush_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpush.Click
        Dim Presidents(20) As String
        Dim Fnames(20) As String
        Dim reader As IO.StreamReader
        reader = IO.File.OpenText("P:\Presidents.txt")
        For i = 0 To 20
            Presidents(i) = reader.ReadLine
        Next i
        For i = 0 To 20
            lstout.Items.Add(Presidents(i))
        Next i
        For i = 0 To 20
            lstout2.Items.Add()
        Next

    End Sub
End Class


This post has been edited by Helpwithcode1234: 29 March 2011 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

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

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

Re: Simple Error, cannot understand

Posted 29 March 2011 - 07:17 PM

If you stored the full name in the list box then you can simply :) iterate through the items collection, copying each item in turn to a string. Then split the string into a string array. the first entry in the string array will be the first name.
Was This Post Helpful? 0
  • +
  • -

#6 Helpwithcode1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-February 11

Re: Simple Error, cannot understand

Posted 29 March 2011 - 07:20 PM

I understand what you mean, I am just not sure of what the code would be in order to just return the first name strings into the second list box.
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

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

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

Re: Simple Error, cannot understand

Posted 29 March 2011 - 07:43 PM

The easier way might be to copy the file string to a temporary string as you read each line from the file. Populate the list box with the temporary string. Then split the temporary string into a string array. Populate the second list box withe the first element of the array. See the String class split()
Was This Post Helpful? 0
  • +
  • -

#8 Helpwithcode1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-February 11

Re: Simple Error, cannot understand

Posted 29 March 2011 - 07:48 PM

Ok. I did something a little different, I made a second txt file so that it can just read the first names from another file and this is the code:
But an error comes up under the second list box that states "Value cannot be null. Parameter name: item"



Public Class Form1

    Private Sub btnpush_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpush.Click
        Dim Presidents(20) As String
        Dim Fnames(20) As String
        Dim reader As IO.StreamReader
        reader = IO.File.OpenText("P:\Presidents.txt")
        For i = 0 To 20
            Presidents(i) = reader.ReadLine
        Next i
        For i = 0 To 20
            lstout.Items.Add(Presidents(i))
        Next
        reader = IO.File.OpenText("P:\Fnames.txt")
        For i = 0 To 20
            Fnames(i) = reader.ReadLine
        Next i
        For i = 0 To 20
            lstout2.Items.Add(Fnames(i))
        Next

    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

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

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

Re: Simple Error, cannot understand

Posted 29 March 2011 - 08:00 PM

When you are done reading the files close them.
Was This Post Helpful? 1
  • +
  • -

#10 Helpwithcode1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-February 11

Re: Simple Error, cannot understand

Posted 29 March 2011 - 08:06 PM

Would that just be reader.close()?
Was This Post Helpful? 0
  • +
  • -

#11 n8wxs  Icon User is offline

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

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

Re: Simple Error, cannot understand

Posted 29 March 2011 - 08:07 PM

yes

See StreamReader Class

This post has been edited by n8wxs: 29 March 2011 - 08:08 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1