473,388 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

entering file contents into an array

I am a student finishing a class in Visual Basic, and would like some assistance with a program I am working on. I need to read the contents of a file (showing makes of cars on one line and a daily rental fee on the following line) into an array and then display the file contents in a list box. The user then chooses the car he/she wants to rent. I then need to obtain the rental fee from the array and use that value to calculate the total rentals. I have the file writing to an array, and the array going into the listbox, but the display shows blank lines before the first entry. The last file entry is followed by more blank lines, and then a display of system string() on following lines. I cannot seem to find where these extra lines etc are coming from - am sure there is an error in my code somewhere but cannot find it. My code is as follows:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sFileToCheck As String = (Application.StartupPath & "\vehicles.doc")
Dim word As String
Dim items As String()
Dim i As Integer
Dim upperBound As Integer
LstRent.Items.Clear()

If Not File.Exists(Application.StartupPath & "\vehicles.doc") Then
MessageBox.Show("Cannot find the file. Please try again!", "File Error Warning", _
MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If

Dim sr As IO.StreamReader = IO.File.OpenText(Application.StartupPath & "\vehicles.doc")
upperBound = 0
Do While (sr.Peek <> -1)
LstRent.Items.Add(sr.ReadLine)
upperBound += 1
Loop
sr.Close()

ReDim items(upperBound) ' states size of array and adds entries from the file
sr = IO.File.OpenText(Application.StartupPath & "\vehicles.doc")
For i = 1 To upperBound
items(i) = sr.ReadLine
Next
sr.Close()
LstRent.Items.Add(items)

DateRange1.begindate = "Pick-up Date:"
DateRange1.enddate = "Return Date:"
End Sub

I know I need to get the index of the car the user selected, and then get the index of the price by using adding one to the car index, but am not sure how to do that part of the code. Any suggestions would be welcome!!
Nov 29 '06 #1
2 2980
I am a student finishing a class in Visual Basic, and would like some assistance with a program I am working on. I need to read the contents of a file (showing makes of cars on one line and a daily rental fee on the following line) into an array and then display the file contents in a list box. The user then chooses the car he/she wants to rent. I then need to obtain the rental fee from the array and use that value to calculate the total rentals. I have the file writing to an array, and the array going into the listbox, but the display shows blank lines before the first entry. The last file entry is followed by more blank lines, and then a display of system string() on following lines. I cannot seem to find where these extra lines etc are coming from - am sure there is an error in my code somewhere but cannot find it. My code is as follows:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sFileToCheck As String = (Application.StartupPath & "\vehicles.doc")
Dim word As String
Dim items As String()
Dim i As Integer
Dim upperBound As Integer
LstRent.Items.Clear()

If Not File.Exists(Application.StartupPath & "\vehicles.doc") Then
MessageBox.Show("Cannot find the file. Please try again!", "File Error Warning", _
MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If

Dim sr As IO.StreamReader = IO.File.OpenText(Application.StartupPath & "\vehicles.doc")
upperBound = 0
Do While (sr.Peek <> -1)
LstRent.Items.Add(sr.ReadLine)
upperBound += 1
Loop
sr.Close()

ReDim items(upperBound) ' states size of array and adds entries from the file
sr = IO.File.OpenText(Application.StartupPath & "\vehicles.doc")
For i = 1 To upperBound
items(i) = sr.ReadLine
Next
sr.Close()
LstRent.Items.Add(items)

DateRange1.begindate = "Pick-up Date:"
DateRange1.enddate = "Return Date:"
End Sub

I know I need to get the index of the car the user selected, and then get the index of the price by using adding one to the car index, but am not sure how to do that part of the code. Any suggestions would be welcome!!

Are you using a microsoft word document to read from, eg "vehicles.doc"?
Normally I would only use a text file to read from, this could explain your blank lines. Otherwise read the line into a variable then test to see if its blank before adding it to the array eg

strTemp = sr.ReadLine

If Trim(strTemp) <> "" Then
LstRent.Items.Add(strTemp)
End If


Both the index on the list box and the array should be zero based so if the have the same order you should just have to reference the Listbox.SelectedIndex.

If the user selects the
1st car in the list Listbox.SelectedIndex = 0
2nd car in the list Listbox.SelectedIndex = 1
etc etc

So in the event you use after the selection you could do
Current Car = items(0, Listbox.SelectedIndex)
Current Car Price = items(1, Listbox.SelectedIndex)
Nov 30 '06 #2
Thank you so much for your message. I will try your suggestions this afternoon and let you know what happens.
Nov 30 '06 #3

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

Similar topics

6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
2
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
0
by: harish139 | last post by:
hi, i want to create a xml file dynamically, that is i have 2 arrays, i want to run a program to fill that array, if the array size becomes 2KB i want to create a xml file like this ...
2
by: Patrick | last post by:
Hi All, I have numerous files that look like the chunk below. From the date and time I calculate a year day value. For the example that value is 148.67721064815 I'm not an expert yet at...
2
by: reyalp | last post by:
I use C++ Builder to create a simple project that can open a picture and save some text information to a file My question is that: After I execute the open picture dialog(btn_OpenPicture) and...
7
by: theballz | last post by:
Hi, I am learning c programming and come across a problem i cant seem to solve. I have a file which i wish to parse and put certain lines (which do not contain a hash character) into an array...
0
by: programming | last post by:
hi all, here is the php code! i am trying to use to 'edit' hyperlink to edit txt in member.txt. Problem is that it reads the values ok into an array, however when i attempt and update new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...

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.