Visual Basic使用从文本文件读入的数据对数组进行排序

时间:2016-04-06 19:57:22

标签: arrays vb.net visual-studio sorting text-files

我遇到了这个问题:

btnDisplay_Click过程应该读取states.txt文件中包含的五个名称,将每个名称存储在一个五元素的一维数组中。该过程应按降序对数组进行排序,然后在列表框中显示数组的内容。

使用我的代码,我可以在列表框中显示5个州名,但是它们没有被排序。

代码的第一次迭代(旧):

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

我也试过将分类线放在循环中。如何让它在列表框中显示已排序的数组?

第二次代码重复(最新):

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

2 个答案:

答案 0 :(得分:0)

更新If语句:

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

答案 1 :(得分:0)

我得到了,感谢你们的帮助!对于我的上一期,我刚刚补充道:

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

所以最终的工作代码如下所示:

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

谢谢你们!

相关问题