2011-10-02 23 views
1

所以我試圖從兩個文本文件中提取一些數據。第一次加載並完美地填充列表框。第二個文本文件是我遇到麻煩的地方。我似乎無法正確加載它。我正在嘗試將其放入二維數組MileageAr中。如果消息框正確加載到陣列中,則該消息框將進行故障排除。我究竟做錯了什麼?從SDMileage.dat來自.dat文件的Visual Basic 2d數組

1,54,465,58,488,484 
5,54,654,87,841,844 

示例數據....

Public Class ArrayFun 
Dim MileageAr(10, 10) As String 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Dim State As New System.IO.StreamReader("SDCities.dat") 
    Dim strState As String 
    Dim i, j As Integer 
    Do While State.Peek <> -1 
     strState = State.ReadLine 
     lstFrom.Items.Add(strState) 
     lstTo.Items.Add(strState) 
    Loop 


    Dim Mileage As New System.IO.StreamReader("SDMileage.dat") 
    Dim strLine As String 
    Dim strSplit() As String 

    Do While State.Peek <> -1 
     strLine = Mileage.ReadLine 
     strSplit = strLine.Split(",") 
     j = 0 
     For Each miles In strSplit 
      MileageAr(i, j) = miles 
      j += 1 
     Next 
     i += 1 
    Loop 
    State.Close() 
    Mileage.Close() 

End Sub 


Private Sub lstTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstTo.SelectedIndexChanged 
    MsgBox(MileageAr(1, 1)) 
End Sub 

末級

+0

它拋出一個錯誤? – adatapost

回答

1
Dim Mileage As New System.IO.StreamReader("SDMileage.dat") 
Dim strLine As String 
Dim strSplit() As String 

更改爲Mileage.Peek

Do While State.Peek <> -1 
    strLine = Mileage.ReadLine 
    strSplit = strLine.Split(",") 

循環嘗試這種方式,而不是

Dim Mileage As System.IO.TextReader = New StreamReader("SDMileaage.dat") 

do while ((strline = Mileage.ReadLine) <> nothing) 

Peek方法可能會被罰款,我只是通常使用的文本文件時,上面的代碼,可能是值得一試...

+0

良好的捕捉,但它仍然不起作用 – atrueresistance

+0

您是否收到錯誤?究竟發生了什麼? – Sparky

+0

沒有錯誤,我似乎無法從數組中得到任何輸出,這意味着它沒有正確填充 – atrueresistance