1

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?

jth41
  • 3,808
  • 9
  • 59
  • 109
  • I see various problems with your code. Can you please, be a bit more precise in the exact error you are getting and in which line? – varocarbas Nov 19 '13 at 19:45
  • What do you mean by "break"? Just quit processing? – Joel Coehoorn Nov 19 '13 at 19:46
  • `Dim cleanline = line.Trim()` makes a `" "` equal to `""` and thus with Length = 0 what provokes the problem while intending to access via substring an inexistent index. You should always check for length = 0 and null strings before performing any string analysis. Joel Coehoorn has updated his answer right now with a suggestion on this front (you can replace his `Exit` with `Continue` or put the whole analysis inside a condition). Also next time, please, be more clear (error text and/or exact line is pretty helpful). – varocarbas Nov 19 '13 at 19:58
  • @varocarbas testing the suggestions yall have made. not sure what you mean by the politeness comment? – jth41 Nov 19 '13 at 20:03
  • I meant not answering to someone intending to help you (now I am bit tired and I am less patient on this front... sorry... last days have been a jokish nightmare). The one writing the suggestion was Joel, I have just recommended you a variation of his code adapted to your needs. The problem here is that you are accessing null/zero-length strings, once this is clear; you should be able to fix it by your own. Joel provided you a code which, if does not deliver the exact functionality you are after, you should adapt it to your exact requirements. – varocarbas Nov 19 '13 at 20:06
  • No I am very appreciative of the help. I was just reading the documentation on `continue` I wanted to be sure I understood and not just copying code. sorry for taking too long to respond – jth41 Nov 19 '13 at 20:10

3 Answers3

2

Replace this:

System.Console.Write(vbLf)

With this:

Console.WriteLine()

For Each line As String In lines
   If line.IsNullOrWhiteSpace() Then Exit For
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

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

Jeremy Lin
  • 400
  • 3
  • 13
0

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
Zacharious
  • 515
  • 4
  • 13