User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 375,199 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,153 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser:
Views: 728 | Replies: 6
Reply
Join Date: May 2008
Posts: 5
Reputation: Sliders is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sliders's Avatar
Sliders Sliders is offline Offline
Newbie Poster

Loading A Text File into a Array into a Listbox

  #1  
May 8th, 2008
I am usually not keen for asking for help in these kind of situations but I am really stumped when it comes to this, all I really need is a good step in the right direction and I should be (hopefully) fine.

I am currently writing an airplane seating program for my VB class. What I am trying to do is load a text file into an array and then out put it into the list box.

  1. Dim seat(15, 6) As Double
  2. Dim sr As IO.StreamReader = IO.File.OpenText("plane.txt")
  3. For row As Integer = 0 To 6
  4. For col As Integer = 0 To 15
  5. seat(row, col) = CDbl(sr.ReadLine)
  6. Next
  7. Next
  8. listSeat.Items.Clear()
  9. listSeat.Items.Add("ABC DEF")
  10. Do While sr.Peek <> -1
  11. listSeat.Items.Add(sr.ReadLine)
  12. Loop
  13. sr.Close()
  14. End Sub

The setup for the txt file is like this:

... ...
... ...
... ...
... ...
... ...
... ...
... ...
... ...
... ...
... ...
... ...
... ...
... ...
... ...
... ...

When I execute the program I get a InvalidCastException which said {"Conversion from string "V.. ..." to type 'Double' is not valid."}

The "V.. ...." is from the txt file, from this error from what I am trying to comprehend is that its trying to convert the whole line to double, but I am not sure on how to go about going fixing this small problem, because If i can figure this out then I can figure out the rest of my problem.

Thanks in advanced
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Posts: 664
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 92
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Master Poster

Re: Loading A Text File into a Array into a Listbox

  #2  
May 8th, 2008
Hi,

Not sure, try this, change your code:

listSeat.Items.Add(sr.ReadLine( ))

Regards
Veena
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: Sliders is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sliders's Avatar
Sliders Sliders is offline Offline
Newbie Poster

Re: Loading A Text File into a Array into a Listbox

  #3  
May 8th, 2008
No, that didn't work. What also I tried to do is reverse the do while statement and the for statement but I got an out of bounds exception.
Reply With Quote  
Join Date: Nov 2006
Posts: 664
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 92
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Master Poster

Re: Loading A Text File into a Array into a Listbox

  #4  
May 9th, 2008
Hi,

Try to declare reader oject like this :

Dim sr As New System.IO.StreamReader("C:\plane.txt")

Give complete path of the file..

REgards
Veena
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: Sliders is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sliders's Avatar
Sliders Sliders is offline Offline
Newbie Poster

Re: Loading A Text File into a Array into a Listbox

  #5  
May 9th, 2008
No I shouldn't have to give the complete path since the txt file is already where it is supposed to be in my project file, it will load just fine, but from what I see the problem is the way the code is written, and I have really no idea how to fix it.
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: Sliders is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sliders's Avatar
Sliders Sliders is offline Offline
Newbie Poster

Re: Loading A Text File into a Array into a Listbox

  #6  
May 9th, 2008
Sorry for double posting but I think I fixed the code to make it better, and it works to an extent.

Private Sub Chart_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sr As IO.StreamReader = IO.File.OpenText("plane.txt")
        For row As Integer = 0 To 15
            For col As Integer = 0 To 6
                seat(row, col) = CStr(sr.ReadLine)
            Next
        Next
        listSeat.Items.Add("ABC DEF")
        'Do While sr.Peek <> -1
        ''''listSeat.Items.Add(seat(row, col))
        sr.Close()
    End Sub

I don't get any out of bounds errors or null exception errors, but it won't output the text file into the list box for some reason
Reply With Quote  
Join Date: Nov 2006
Posts: 664
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 92
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Master Poster

Re: Loading A Text File into a Array into a Listbox

  #7  
May 12th, 2008
Hi,

While populating the "Seat", populate the Listbox as well...

  1. Private Sub Chart_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. Dim sr As IO.StreamReader = IO.File.OpenText("plane.txt")
  3. listSeat.Items.Add("ABC DEF")
  4. For row As Integer = 0 To 15
  5. For col As Integer = 0 To 6
  6. seat(row, col) = CStr(sr.ReadLine)
  7. listSeat.Items.Add(seat(row, col))
  8. Next
  9. Next
  10. sr.Close()
  11. End Sub

Regards
Veena
Last edited by QVeen72 : May 12th, 2008 at 7:22 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 2:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC