-
Apr 2nd, 2017, 12:21 PM
#1
Thread Starter
Member
[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.
-
Apr 2nd, 2017, 02:52 PM
#2
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.
-
Apr 2nd, 2017, 09:43 PM
#3
Thread Starter
Member
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
-
Apr 3rd, 2017, 10:46 AM
#4
Re: Problem Editing Text File
Originally Posted by Cow124
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)
-
Apr 3rd, 2017, 01:34 PM
#5
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|