Program reading strings wrong?
If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
2 posts
Page 1 of 1
So I have a text file that contains every word on this list ("C:\ListOfWords.txt") and I have a program with this code:
Now 'hello' should have 3 hits because it has the h,e, and o, but it doesn't.
Any help is appreciated, thanks
Code: Select all
After I run the program it will give me
Dim allWords As New Specialized.StringCollection
Sub Main()
Dim hits As Integer = 0
Dim sw As IO.StreamWriter = New IO.StreamWriter("C:\log.txt")
allWords.AddRange(IO.File.ReadAllLines("C:\ListOfWords.txt"))
Dim theLetters() As String = {"h", "e", "?", "?", "o"}
For Each i As String In allWords
If Not i.Length <> theLetters.Count Then
For k As Integer = 0 To theLetters.Count - 1
If i(k) = theLetters(k) Then
hits += 1
End If
Next
If hits = 2 Then
Console.WriteLine(i & " - " & hits)
sw.WriteLine(i & " - " & hits)
hits = 0
Else : hits = 0
End If
End If
Next
sw.Close()
Console.Read()
End Sub
Cello - 2in the log.
Gecko - 2
Hello - 2
Negro - 2
Tempo - 2
Zeppo - 2
Now 'hello' should have 3 hits because it has the h,e, and o, but it doesn't.
Any help is appreciated, thanks

I believe the problem is it's checking for lower case letters in here..
Just a quick edit but try changing this:
Code: Select all
But some of your words contain upper case letters.Dim theLetters() As String = {"h", "e", "?", "?", "o"}
Just a quick edit but try changing this:
Code: Select all
To this:If i(k) = theLetters(k) Then
Code: Select all
See if that helps :?If i(k).ToString.ToLower = theLetters(k) Then
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
2 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023