Results 1 to 5 of 5

Thread: [RESOLVED] Problem Editing Text File

  1. #1

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Resolved [RESOLVED] Problem Editing Text File

    Hi everyone!
    (VS Community 2015)
    I am trying to edit a word list (alphabetical order) text file encoded in UTF-8 (separated by enter/returns (one word per line)). The program's purpose is to remove any words with over 18 characters (>18, so 1-18 letters inclusive) and put the new list of text into a new text file.
    The error message right now is:
    An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll. Additional information: Conversion from string "a" to type 'Integer' is not valid.
    On this line:
    Code:
    If Len(Console.ReadLine(lines.Item(line))) > 18 Then
    Here is the full code:
    Code:
    Imports System.IO
    
    Public Class Form1
    
    
        Dim strmRdr As New StreamReader("C:\Users\User\Documents\DictionaryReviser\DictionaryReviser\Resources\wordsEnutf8.txt")
        Dim lines As New List(Of String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            While strmRdr.Peek <> -1
                lines.Add(strmRdr.ReadLine())
            End While
    
            For line = 0 To (lines.Count)
                If Len(Console.ReadLine(lines.Item(line))) > 18 Then
                    lines.RemoveAt(line)
                End If
            Next
            strmRdr.Close()
            strmRdr.Dispose()
            System.IO.File.WriteAllLines("C:\Users\User\Documents\DictionaryReviser\DictionaryReviser\Resources\dictionaryrevised.txt", lines)
        End Sub
    End Class
    Thanks in advance.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Problem Editing Text File

    Think a bit more carefully about what that line of code is doing...

    line is an Integer (with the value set by the loop), and lines.Item(line) will return a String from the list (which one is based on the value of line).

    Assuming the String you have at this point is "a", the code Console.ReadLine(lines.Item(line))) is the same as Console.ReadLine("a") , which isn't valid, and doesn't make much sense.

    I suspect that putting in the Console.ReadLine() was a mistake (perhaps you forgot to remove it as you modified the code to use the List), and that removing it will fix the problem.

  3. #3

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Problem Editing Text File

    Thank you for that answer, as it solved that problem, but now I have another problem.
    On this line of code:
    Code:
    If Len(lines.Item(line)) > 18 Then
    This error shows up:
    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
    The error happens at this time:
    Code:
    lines.count = 109517
    line = 109517
    Changing the for loop to : 0 to 109516 solves the problem, but in the future, how should I approach this?
    Thanks

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Problem Editing Text File

    Quote Originally Posted by Cow124 View Post
    The error happens at this time:
    Code:
    lines.count = 109517
    line = 109517
    Changing the for loop to : 0 to 109516 solves the problem, but in the future, how should I approach this?
    Thanks
    Collections are zero indexed based, so the first element is at index 0,
    For line = 0 To
    ,and the last element is equal to the count minus one,
    For line = 0 To (lines.Count - 1)

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Problem Editing Text File

    I would also like to point out that your problem could be solved in 3 lines by utilizing the .NET framework's LINQ. Here would be an example:
    Code:
    Dim words() As String = IO.File.ReadAllLines("C:\Users\User\Documents\DictionaryReviser\DictionaryReviser\Resources\wordsEnutf8.txt")
    words = words.Where(Function(word) word.Length < 18).ToArray()
    
    IO.File.WriteAllLines("C:\Users\User\Documents\DictionaryReviser\DictionaryReviser\Resources\dictionaryrevised.txt", words)
    What this code does is first read all lines from the designated text file, then it selects only those words who's Length is less than 18, and then it writes the modified collection to the new designated text file.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width