I am trying to read in a text file (derived from the Mac dictionary file) line by line, and add words of a defined length into a listbox. The overall idea is to end up with an array of more or less random words of a specified length.
It’s not working. Regardless of the length I define, all of the words in the file are read in. Also, I thought it would make sense to record the length of each line too. But the number doesn’t reflect the actual length of the word on that line. I’m clearly doing something wrong, but I can’t figure it out. The code is below. Can anyone tell me where I’m going wrong? Or is there a better way to do this?
Code follows:
Using Xojo.Core
Using Xojo.IO
Dim wordsize As Integer
Dim f As FolderItem
f = SpecialFolder.Documents.Child("wordlist.txt")
wordsize = txtWordSize.Text.Val
If Not f.Exists Then
' Cannot read the file if it does not exist
Return
End If
Dim errorText As Text
Try
Dim input As TextInputStream
input = TextInputStream.Open(f, TextEncoding.UTF8)
While Not input.EOF
if input.ReadLine.length = wordsize then
lstMasterWordList.AddRow(input.ReadLine + " : " + str(input.readline.length) + " :")
else
// do nothing
end if
Wend
input.Close
Catch e As IOException
errorText = "File IO Error: " + e.Reason
End Try