Reading the length of a line

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
End If

Dim errorText As Text

  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) + " :")
      // do nothing
    end if
Catch e As IOException
  errorText = "File IO Error: " + e.Reason
End Try

@Ian Piper — You are using “input.ReadLine” several times as if it would return the same value each time but in reality, it will read the next word each time you use it. You must store the first value in a variable, e.g.

dim word as String = input.ReadLine //<<< Use ReadLine only once if word.length = wordsize then lstMasterWordList.AddRow(word + " : " + str(word.length) + " :") else // do nothing end if

Your code above returns 5 errors while trying to compile with Xojo 2018r4.

@StphaneMons - Thank you. That did it.