473,585 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

entering file contents into an array

22 New Member
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(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim sFileToCheck As String = (Application.St artupPath & "\vehicles.doc" )
Dim word As String
Dim items As String()
Dim i As Integer
Dim upperBound As Integer
LstRent.Items.C lear()

If Not File.Exists(App lication.Startu pPath & "\vehicles.doc" ) Then
MessageBox.Show ("Cannot find the file. Please try again!", "File Error Warning", _
MessageBoxButto ns.OK, MessageBoxIcon. Warning)
End If

Dim sr As IO.StreamReader = IO.File.OpenTex t(Application.S tartupPath & "\vehicles.doc" )
upperBound = 0
Do While (sr.Peek <> -1)
LstRent.Items.A dd(sr.ReadLine)
upperBound += 1
Loop
sr.Close()

ReDim items(upperBoun d) ' states size of array and adds entries from the file
sr = IO.File.OpenTex t(Application.S tartupPath & "\vehicles.doc" )
For i = 1 To upperBound
items(i) = sr.ReadLine
Next
sr.Close()
LstRent.Items.A dd(items)

DateRange1.begi ndate = "Pick-up Date:"
DateRange1.endd ate = "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 2990
Nathan G
2 New Member
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(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim sFileToCheck As String = (Application.St artupPath & "\vehicles.doc" )
Dim word As String
Dim items As String()
Dim i As Integer
Dim upperBound As Integer
LstRent.Items.C lear()

If Not File.Exists(App lication.Startu pPath & "\vehicles.doc" ) Then
MessageBox.Show ("Cannot find the file. Please try again!", "File Error Warning", _
MessageBoxButto ns.OK, MessageBoxIcon. Warning)
End If

Dim sr As IO.StreamReader = IO.File.OpenTex t(Application.S tartupPath & "\vehicles.doc" )
upperBound = 0
Do While (sr.Peek <> -1)
LstRent.Items.A dd(sr.ReadLine)
upperBound += 1
Loop
sr.Close()

ReDim items(upperBoun d) ' states size of array and adds entries from the file
sr = IO.File.OpenTex t(Application.S tartupPath & "\vehicles.doc" )
For i = 1 To upperBound
items(i) = sr.ReadLine
Next
sr.Close()
LstRent.Items.A dd(items)

DateRange1.begi ndate = "Pick-up Date:"
DateRange1.endd ate = "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.d oc"?
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.A dd(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.Selecte dIndex.

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

So in the event you use after the selection you could do
Current Car = items(0, Listbox.Selecte dIndex)
Current Car Price = items(1, Listbox.Selecte dIndex)
Nov 30 '06 #2
lab3terch
22 New Member
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
5627
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 that array back into another file. I've tried, but i can only read the data and place it on the screen, i cant get it into an array. Any help...
7
3527
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. How do i make the last pointer in the indirect sector that has another level of indirect pointer, and be defined recursively to support infinite...
2
15983
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 script. Now I want to delete a line from a file. Line being the strings I got/saved to/from array of arrays.
29
10397
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 then print out the contents of the 2d array to the screen. I wil also need to append data to the .dat file but mostly right now I'm worrying about...
0
2500
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 <detail> <person>
2
1518
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 dealing with arrays, but I get around well in php otherwise (reading data files, loops, functions, etc). My question is how would I go about creating...
2
3722
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 open the picture in the form, the write to file function(btn_SaveClick) doesn't work anymore. Every time I click the save button, nothing changed in...
7
3583
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 and then output the contents of this array. The file seems to be parsed properly and the array gets populated but when I output the array the last...
0
4489
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 values into the file, i get no results. Does anybody know where i am going wrong???? <? session_start(); // Set updated content for user
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.