entering file contents into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lab3terch
    New Member
    • Oct 2006
    • 22

    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(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!!
  • Nathan G
    New Member
    • Nov 2006
    • 2

    #2
    Originally posted by lab3terch
    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)

    Comment

    • lab3terch
      New Member
      • Oct 2006
      • 22

      #3
      Thank you so much for your message. I will try your suggestions this afternoon and let you know what happens.

      Comment

      Working...