2016-04-06 10 views
1

Ich habe Probleme mit diesem:Visual Basic Sortierung ein Array mit eingelesenen Daten aus Textdatei

Die btnDisplay_Click Verfahren sollte die fünf Namen in der Datei enthaltenen states.txt lesen, speichern jeweils in einem fünf- Element eindimensionales Array. Die Prozedur sollte das Array in absteigender Reihenfolge sortieren und dann den Inhalt des Arrays im Listenfeld anzeigen.

Mit meinem Code kann ich die 5 Zustandsnamen in der Listbox anzeigen lassen, sie werden jedoch nicht sortiert.

erste Iteration CODE (alt):

Public Class frmMain 

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click 

    'Declare an array for 5 states 
    Dim strStates(4) As String 

    Dim strStateName As String 

    'Sort the array in descending order 
    Array.Sort(strStates) 
    Array.Reverse(strStates) 

    'Declare variable to hold stream reader object 
    Dim inFile As IO.StreamReader 

    'Check if txt file exists before opening to avoid run time error/crash 
    If IO.File.Exists("states.txt") Then 
     'Open the file 
     inFile = IO.File.OpenText("states.txt") 
     'Loop instructions until end of file is reached 
     Do Until inFile.Peek = -1 
      'Read a line 
      strStateName = inFile.ReadLine 
      'Add line (state) to list box 
      lstNames.Items.Add(strStateName) 
     Loop 
     'Close the file 
     inFile.Close() 
    Else 
     'Show a message box telling user file can't be found 
     MessageBox.Show("File does not exist or cannot be found.", "States", MessageBoxButtons.OK, MessageBoxIcon.Information) 
    End If 
End Sub 

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click 
    Me.Close() 
End Sub 
End Class 

habe ich versucht, als auch die Sortierlinien innerhalb der Schleife setzt. Wie bekomme ich dies, um das sortierte Array in der Listbox anzuzeigen?

zweite Iteration CODE (aktuellen):

Public Class frmMain 

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click 

    'Declare an array to hold all 5 states 
    Dim strStates(4) As String 

    'Declare variable to hold loop counts 
    Dim i As Integer = 0 

    'Declare variable to hold stream reader object 
    Dim inFile As IO.StreamReader 

    'Check if txt file exists before opening to avoid run time error/crash 
    If IO.File.Exists("states.txt") Then 
     'Open the file 
     inFile = IO.File.OpenText("states.txt") 
     'Loop instructions until end of file is reached 
     Do Until inFile.Peek = -1 
      'Read a line and store in array 
      strStates(i) = inFile.ReadLine 

      'Message box to confirm array loop is working correctly 
      MessageBox.Show(strStates(i)) 

      'Manually increment array counter 
      i = i + 1 
     Loop 

     'Close the file 
     inFile.Close() 

     'Sort the array in descending order 
     Array.Sort(strStates) 
     Array.Reverse(strStates) 

     'Output to list box 
     lstNames.Items.Add(strStates(i)) 'error thrown here 

    Else 
     'Show a message box telling user file can't be found 
     MessageBox.Show("File does not exist or cannot be found.", "States", MessageBoxButtons.OK, MessageBoxIcon.Information) 
    End If 
End Sub 
+0

Sie sortieren das Array, bevor Sie etwas einfügen. – Plutonix

+0

Ja, es scheint, dass das Problem tatsächlich darin liegt, die Wörter aus der Textdatei in das Array zu bekommen. Ich habe gerade erkannt, dass der Code das nicht tut (daher gibt es nichts zu sortieren). Es setzt die Wörter Zeile für Zeile in das Listenfeld. Ich arbeite immer noch daran, aber jede Hilfe wird geschätzt. –

+0

Tun Sie sich einen Gefallen und entfernen Sie das Array. Verwenden Sie stattdessen eine 'List (of string)'. Es dauert nur 5 Minuten, um zu lernen, wie man sie benutzt. Eine der Möglichkeiten, wie sie besser sind, ist, dass Sie nicht wissen müssen, wie groß sie sind: Sie messen sich selbst. Fügen Sie dann 'strStateName' zur Liste hinzu. Verwenden Sie am Ende die Liste als Datenquelle: 'lstNames.DataSource = myNameList'. Stellen Sie sicher, dass Sie die Liste außerhalb dieser Methode deklarieren. – Plutonix

Antwort

0

Aktualisieren Sie die If-Anweisung:

If IO.File.Exists("states.txt") Then 
    'Open the file 
    inFile = IO.File.OpenText("states.txt") 
    'Loop instructions until end of file is reached 
    Do Until inFile.Peek = -1 
     'Read a line and store in array 
     strStates(i) = inFile.ReadLine 

     'Manually increment array counter 
     i = i + 1 
    Loop 

    'Close the file 
    inFile.Close() 

    'Sort the array in descending order (Next 2 lines don't work) 
    Array.Sort(strStates) 
    Array.Reverse(strStates) 

    'Output to list box 
    lstNames.Items.Add(strStates(i)) 

Else 
    'Show a message box telling user file can't be found 
    MessageBox.Show("File does not exist or cannot be found.", "States", MessageBoxButtons.OK, MessageBoxIcon.Information) 
End If 
+0

Dies löst einen Fehler aus: "IndexOutOfRangeException wurde nicht behandelt. Zusätzliche Informationen: Index war außerhalb der Grenzen des Arrays." in der Zeile, die das Array zum Listenfeld hinzufügt [lstNames.Items.Add (strStates (i))].Wenn ich jedoch das i durch eine Zahl wie 0 bis 4 ersetze, wird der entsprechende Zustand angezeigt, aber nicht alle. –

+0

Ja, AddRange ist der Weg zum Laden der Listbox-Elemente. Das tut mir leid. – JerryM

0

Ich habe es, dank der Hilfe von zwei! für meine letzte Frage, ich habe gerade hinzugefügt:

 For i = 0 To strStates.Length - 1 
      lstNames.Items.Add(strStates(i)) 
     Next i 

So ist der letzte Arbeits Code wie folgt aussieht:

Public Class frmMain 

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click 

    'Declare an array to hold all 5 states 
    Dim strStates(4) As String 

    'Declare variable to hold loop counts 
    Dim i As Integer = 0 

    'Declare variable to hold stream reader object 
    Dim inFile As IO.StreamReader 

    'Check if txt file exists before opening to avoid run time error/crash 
    If IO.File.Exists("states.txt") Then 
     'Open the file 
     inFile = IO.File.OpenText("states.txt") 
     'Loop instructions until end of file is reached 
     Do Until inFile.Peek = -1 
      'Read a line and store in array 
      strStates(i) = inFile.ReadLine 

      'Message box to confirm array loop is working correctly 
      'MessageBox.Show(strStates(i)) 

      'Manually increment array counter 
      i = i + 1 
     Loop 

     'Close the file 
     inFile.Close() 

     'Sort the array in descending order 
     Array.Sort(strStates) 
     Array.Reverse(strStates) 

     'Output to list box 
     For i = 0 To strStates.Length - 1 
      lstNames.Items.Add(strStates(i)) 
     Next i 

    Else 
     'Show a message box telling user file can't be found 
     MessageBox.Show("File does not exist or cannot be found.", "States", 
     MessageBoxButtons.OK, MessageBoxIcon.Information) 
    End If 
End Sub 

Danke Jungs!

+1

'lstNames.Items.AddRange (strStates)' ist alles was Sie brauchen, um das Array hinzuzufügen. Haben Sie nie Angst, die Liste der Methoden für ein Objekt zu erforschen, um zu sehen, was es sonst tut. – Plutonix