从分隔的文本文件中选择随机字符串

时间:2014-03-27 20:37:14

标签: arrays vb.net vba random

我正在尝试将所有已解析的字符串输入到2D数组中,其中前5个字符串组成一列,然后接下来的5个组成第二列等。然后我需要随机选择3列并输出该列中的每个字符串。

        path = My.Computer.FileSystem.GetFileInfo("Two_Point_One_Questions.txt")
    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(path.FullName)
        MyReader.TextFieldType = FileIO.FieldType.Delimited
        MyReader.SetDelimiters(",")
        Dim currentRow As String()
        While Not MyReader.EndOfData
            Try
                currentRow = MyReader.ReadFields()
                Dim currentField As String
                Dim i As Integer = 1
                Dim j As Integer = 1
                For Each currentField In currentRow
                    Question(i, j) = currentField
                    j = j + 1
                Next
            Catch ex As Microsoft.VisualBasic.
                        FileIO.MalformedLineException
                MsgBox("Line " & ex.Message &
                "is not valid and will be skipped.")
            End Try
        End While
    End Using

1 个答案:

答案 0 :(得分:0)

我不确定你要做什么,但你需要增加变量i来填充问题数组的新插槽

        ....
        End Try
        i = i + 1
    End While
End Using

顺便说一下,数组从索引零开始,而不是从一开始。所以可能你对i,y的陈述值是错误的

Dim i As Integer = 0
Dim j As Integer = 0

最后,变量i应在进入while循环之前声明并初始化,否则它将被重置为其初始值,从文件读取的行将始终到达数组的同一插槽