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.
2 posts Page 1 of 1
Contributors
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

Program reading strings wrong?
SumCode
So I have a text file that contains every word on this list ("C:\ListOfWords.txt") and I have a program with this code:
Code: Select all
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
After I run the program it will give me
Cello - 2
Gecko - 2
Hello - 2
Negro - 2
Tempo - 2
Zeppo - 2
in the log.

Now 'hello' should have 3 hits because it has the h,e, and o, but it doesn't.

Any help is appreciated, thanks :)
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

I believe the problem is it's checking for lower case letters in here..
Code: Select all
Dim theLetters() As String = {"h", "e", "?", "?", "o"}
But some of your words contain upper case letters.

Just a quick edit but try changing this:
Code: Select all
If i(k) = theLetters(k) Then
To this:
Code: Select all
If i(k).ToString.ToLower = theLetters(k) Then
See if that helps :?
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
2 posts Page 1 of 1
Return to “Coding Help & Support”