使用 ArrayList 将文本文件拆分并存储到数组中 [英] Split and Store a text file into an array using ArrayList
问题描述
我一直在开发一个测验应用程序,它使用文本文件来存储问题.
I have been developing a quiz application that uses a textfile to store questions.
问题采用这种格式"QUESTION##CHOICE_A##CHOICE_B##CHOICE_C##CHOICE_D##ANSWER"
我希望它读取每一行使用 "##"
作为拆分字符串将其拆分为 6 个不同的部分并将其存储到数组中,例如 Questions, CHOICE_A,CHOICE_B,CHOICE_C,CHOICE_D代码>
The questions are formatted in this format "QUESTION##CHOICE_A##CHOICE_B##CHOICE_C##CHOICE_D##ANSWER"
I want it to read each line splits it into 6 different parts using "##"
as the split string and store it into arrays such as Questions, CHOICE_A,CHOICE_B,CHOICE_C,CHOICE_D
我的代码没有循环.它只存储第一行
My code does not loop. it just stores the first line
问题下方有图示
这是我的代码
Dim sr As StringReader = New StringReader(My.Resources.ResourceManager.GetObject(globalVariables.currSubject))
Dim questions As String
Dim splitquestions(6) As String
Dim Unsplitquestions(6) As String
Dim i As Integer = 0
Do Until sr.Peek = -1
questions = sr.ReadLine
Unsplitquestions(i) = questions
splitquestions = Unsplitquestions(i).Split(New String() {"##"}, StringSplitOptions.RemoveEmptyEntries)
' Splits and Stores Into Various
'
'
globalVariables.ArrayQuestions.Add(splitquestions(0))
globalVariables.optionA.Add(splitquestions(1))
globalVariables.optionB.Add(splitquestions(2))
globalVariables.optionC.Add(splitquestions(3))
globalVariables.optionD.Add(splitquestions(4))
globalVariables.Answer.Add(splitquestions(5))
Loop
推荐答案
不,不要使用 ArrayList 来操作一组这样的对象.您应该尝试以面向对象的方式思考.QuestionEntry 是一个实体,其中包含一个 QuestionText、4 个可能的问题答案和一个 QuestionAnswer.
No, do not use an ArrayList for manipulating a set of objects like this. You should try to think in an Object Oriented way. A QuestionEntry is an entity that contains a QuestionText, 4 possible question answers and one QuestionAnswer.
让我们试试这个代码
Public Class QuestionEntry
Public QuestionText as String
Public ChoiceA as String
Public ChoiceB as String
Public ChoiceC as String
Public ChoiceD as String
Public QuestionAnswer as String
End Class
Dim questions = new List(Of QuestionEntry)()
Dim line As String
Do While sr.Peek() >= 0
line = sr.ReadLine
Console.WriteLine(line)
Dim parts = line.Split(New String() {"##"}, StringSplitOptions.RemoveEmptyEntries)
Dim q = new QuestionEntry()
With q
.QuestionText = parts(0)
.ChoiceA = parts(1)
.ChoiceB = parts(2)
.ChoiceC = parts(3)
.ChoiceD = parts(4)
.QuestionAnswer = parts(5)
End With
questions.Add(q)
Loop
当然这只是一个例子,需要进行一些错误检查才能使代码更安全.例如,在创建新问题条目之前,您应该检查拆分返回的数组是否具有有效的 6 个元素.(部分.长度 = 6)
Of course this is just an example and a bit of error checking will be required to make the code more safe. For example before creating the new question entry you should check if the array returned by the split has effectively 6 elements. (parts.Length = 6)
现在您的所有文本都由 List(Of QuestionEntry) 的实例处理,您可以像使用普通数组一样使用它
Now all of your text is handled by an instance of a List(Of QuestionEntry) and you could use it like a normal array
Dim qe = questions(0)
Console.WriteLine("Question: " & qe.QuestionText)
Console.WriteLine("Choice A: " & qe.ChoiceA)
Console.WriteLine("Choice B: " & qe.ChoiceB)
Console.WriteLine("Choice C: " & qe.ChoiceC)
Console.WriteLine("Choice D: " & qe.ChoiceD)
Console.ReadLine("Enter your answer:")
这篇关于使用 ArrayList 将文本文件拆分并存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!