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
Simple Error, cannot understand
Page 1 of 110 Replies - 530 Views - Last Post: 29 March 2011 - 08:07 PM
#1
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"
Replies To: Simple Error, cannot understand
#2
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
#3
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!
#4
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
#5
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.
#6
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.
#7
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()
#8
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"
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
#9
Re: Simple Error, cannot understand
Posted 29 March 2011 - 08:00 PM
When you are done reading the files close them.
#10
Re: Simple Error, cannot understand
Posted 29 March 2011 - 08:06 PM
Would that just be reader.close()?
#11
Re: Simple Error, cannot understand
Posted 29 March 2011 - 08:07 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|