I am not sure what I have done wrong. Essentially I am searching an array for colors. If there is no input, on button click you get an error. If the color does not exist you get an error. If it exists then it tells you. The latter two events are not happening. Any clues out there?

Thanks!

Public Class Form1

    Dim count As Integer
    Dim Crayola() As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByValue As System.EventArgs) Handles MyBase.Load

        'the onload code here
        'declare file object
        Dim Infile As IO.StreamReader = IO.File.OpenText("COLORS.TXT")
        Dim name As String
        Dim i, upperBound As Integer  '<---- used in the loop to control		
        upperBound = 0
        '//open file
        '//read file and count
        '//close file
        Do While (Infile.Peek <> -1)

            name = Infile.ReadLine
            upperBound += 1

        Loop

        Infile.Close()
        '//ReDim file
        ReDim Preserve Crayola(upperBound)
        '//open file
        '//read record and put in array until done
        '//close file
        i = 0
        Do While (Infile.Peek <> -1) And i <= upperBound

            name = Infile.ReadLine
            Crayola(i) = name
            i += 1

        Loop
        count = i - 1
        Infile.Close()
    End Sub

    Private Sub calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculate.Click
        Dim userInput, errorMessage1, errorMessage2, foundMessage As String
        Dim i As Integer
        errorMessage1 = "Sorry that is not a color!"
        errorMessage2 = "Please enter a color choice!"
        foundMessage = "Yes that is a Crayola color!"
        userInput = input.Text
        foundORnot.Text = Crayola(0)

        If userInput = "" Then
            foundORnot.Text = errorMessage2
        Else

            i = 0
            Do While i < count
                If userInput = Crayola(i) Then
                    foundORnot.Text = foundMessage
                Else
                    i += 1
                End If
            Loop

        End If

    End Sub
End Class

Recommended Answers

All 3 Replies

Hi,

Do While i < Crayola.Length - 1
            If (userInput = Crayola(i)) Then
                MsgBox("found")
               Exit Do ' put an exit
            Else
                i = i + 1
            End If
          Loop

I used for and a boolean

Dim found As Boolean = False
        For i = 0 To Crayola.Length - 1
            If (userInput= arr(i)) Then
                found = True


            End If
        Next
        If (found) Then
            MsgBox("found")
        Else
            MsgBox("not found")


        End If

I used "for" and a boolean

Dim found As Boolean = False
        For i = 0 To Crayola.Length - 1
            If (userInput= Crayola(i)) Then
                found = True


            End If
        Next
        If (found) Then
            MsgBox("found")
        Else
            MsgBox("not found")


        End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.