Question

I read through the lines of a text file but break on blank lines.

Using sr As New StreamReader(openFileDialog1.OpenFile())

    Dim text As [String] = sr.ReadToEnd()
    Dim lines As String() = text.Split(vbCrLf)
    For Each line As String In lines

        Dim cleanline = line.Trim()
        Dim words As String() = cleanline.Split(" ")
        For Each word As String In words
            If word.StartsWith("a", True, Nothing) OrElse word.StartsWith("e", True, Nothing) OrElse word.StartsWith("i", True, Nothing) OrElse word.StartsWith("o", True, Nothing) OrElse word.StartsWith("u", True, Nothing) OrElse word.StartsWith("y", True, Nothing) Then
                System.Console.Write(word & "ay ")
            Else
                Dim mutated As String = word.Substring(1, word.Length - 1)
                mutated = mutated & word.Substring(0, 1) & "yay "
                System.Console.Write(mutated)
            End If
        Next
        System.Console.Write(vbLf)

    Next
End Using

I am using this input.

I get this error:

enter image description here

What should I change to prevent this runtime error and continue processing?

Was it helpful?

Solution 2

you should do something like this to ensure that you don't have that error:

...
For Each word As String In words
    If (word.Equals("")) Then
        Continue For
    End If
...

This will ensure you never attempt to translate an empty string

OTHER TIPS

Replace this:

System.Console.Write(vbLf)

With this:

Console.WriteLine()

For Each line As String In lines
   If line.IsNullOrWhiteSpace() Then Exit For

First I used StringSplitOptions.None for lines and words. It will remove the empty string splittings.

Dim lines As String() = text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)

Then I replaced the If statement that checked if word.starsWith({vowels}) with its Regex equivalent. Also, you can use RegexOptions.IgnoreCase to make it case-insensitive. (Import Text.RegularExpressions)

If Regex.IsMatch(word, "^[aeiouy]", RegexOptions.IgnoreCase) Then

Lastly, I added an If to check if word.Length > 0 before trying to access word(1).

Here's my final code:

Using sr As New StreamReader(OpenFileDialog1.OpenFile())
    Dim text As String = sr.ReadToEnd()
    Dim lines As String() = text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
    For Each line As String In lines
        Dim cleanline As String = line.Trim()
        Dim words As String() = cleanline.Split(New Char() {" "c}, StringSplitOptions.None)
        For Each word As String In words
            If Regex.IsMatch(word, "^[aeiouy]", RegexOptions.IgnoreCase) Then
                System.Console.WriteLine(word & "ay ")
            ElseIf word.Length > 0 Then
                Dim mutated As String = word(1) & word(0) & "yay "
                System.Console.WriteLine(mutated)
            End If
        Next
    Next
End Using
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top