0

I'm trying to use this script to compare a users input from a text box with the 22 correct words. I'm not looking for multiple cases, such as VICE is in ADVICE so it would be 2 values; I want it to have the string values to accept only equal values.

At the moment, it is only recognizing the first word TIED and displays a message box "found", but it doesn't not recognize any other word in the list.

I am writing in visual basic script

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim StrCorrect() As String = {"TIED", "VICE", "ICED", "DIVE", "DIET", "DATE", "CITE", "CAVE", "AIDE", "ACED", "CITED", "ACTED", "VACATE", "CATTIE", "ADVICE", "AVIATE", "ACTIVE", "VACATED", "DICTATE", "AVIATED", "ACTIVATE", "ACTIVATED"}
    Dim Find As String = userinput
    For Each Str As String In StrCorrect
        If StrComp(Str, userinput, CompareMethod.Text).ToString = 0 Then
            MsgBox("Found" & userinput)
            Return
        Else : MsgBox("incorrect word")
            Return
        End If
    Next
End Sub
3
  • 1
    You might want to mention the language you are writing this in. visual-studio is not a language. Commented Apr 25, 2013 at 15:29
  • My VB is a little rusty, but don't you want parentheses instead of curly braces? Commented Apr 25, 2013 at 15:38
  • I want it to have the string values to accept only equal values. Equal Values doesn't mean that VICE = Vice Commented Apr 25, 2013 at 16:05

4 Answers 4

1

The problem is that your loop is explicitly returning if the first item isn't a match. You only know you don't have a match if your loop completes without finding one, so try something like this instead:

For Each Str As String In StrCorrect
    If StrComp(Str, userinput, CompareMethod.Text).ToString = 0 Then
        MsgBox("Found" & userinput)
        Return
    End If
Next

MsgBox("incorrect word")

This will only display "incorrect word" if all of the items in your list fail the first test.

Sign up to request clarification or add additional context in comments.

Comments

0

Why STRCOMP? Why not a direct comparision if you want an exact match?

    For Each Str As String In StrCorrect
        If Str = Find Then
            MessageBox.Show("Found :" & Str)
        End If
    Next

2 Comments

Very useful but doesn't solve the problem stated in the question: "it is only recognizing the first word "TIED". Also worth noting that using = omits any culture and case sensitivity options that might be required - it's generally better practise to use a method of the string class for these operations.
It is only supposed to recognize the exact match and not ADVICE See this from the question I want it to have the string values to accept only equal values.
0

Try like below, It will help you...

Sample :

Dim result As String() = Array.FindAll(StrCorrect, Function(s) s.Equals(Find))
If (result.Length > 0) Then
  MsgBox("Found : " & userinput)
Else
  MsgBox("incorrect word")
End If

Full Code :

Dim StrCorrect() As String = {"TIED", "VICE", "ICED", "DIVE", "DIET", "DATE", "CITE", "CAVE", "AIDE", "ACED", "CITED", "ACTED", "VACATE", "CATTIE", "ADVICE", "AVIATE", "ACTIVE", "VACATED", "DICTATE", "AVIATED", "ACTIVATE", "ACTIVATED"}
Dim Find As String = userinput
Dim result As String() = Array.FindAll(StrCorrect, Function(s) s.Equals(Find))
If (result.Length > 0) Then
  MsgBox("Found : " & userinput)
Else
  MsgBox("incorrect word")
End If

Comments

0

I would use a for loop, something like

For i As Integer = 0 To StrCorrect.Length - 1
        If StrCorrect(i) = Find Then
            MsgBox("Found" & Find)
            Return
        'End if

        'The else statement simply alerting that it didnt find the right word on this iteration
        'The else can be removed if you dont want this alert
        Else
            MsgBox("incorrect word")
            'Return
        End If
Next

2 Comments

This doesn't change the outcome of the problem at all.
It loops through the ENTIRE array, the MsgBox stating incorrect word is just alerting...it will stop once it finds the right word...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.