Need Help searching array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jac130
    New Member
    • Oct 2008
    • 18

    Need Help searching array

    the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need to check and make sure that none of hte values have already been entered in the array, if it has..then there must be a message telling user to enter a new value.

    EX: user prompted to enter an integer e.g.-2, then user is prompted to enter values to fill array, it loops until 2 values have been entered- e.g. 1 and 2..then the numbers 1 and 2 are displayed in a list box...but if the user enters 1 and 1..there needs to be a message or input box saying to enter a new value b/c 1 has already been used.

    any help on searching the array would be great, here is my code:


    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    Dim length As String
    length = InputBox("Enter size of array")
    Do While Not ispositiveinteg er(length)
    length = InputBox("Enter size of array")
    Loop



    Dim stringarray() As String
    stringarray = MakeArray(lengt h)
    Dim i As Integer
    For i = 0 To stringarray.Get UpperBound(0)

    lstdisplay.Item s.Add(stringarr ay(i))
    End If

    Next
    End Sub

    Function MakeArray(ByVal size As Integer) As String()
    Dim Array(size - 1) As String
    Dim i As Integer
    For i = 0 To Array.GetUpperB ound(0)
    Array(i) = InputBox("Enter array values: ")
    Next
    Return Array

    End Function
    End Class
  • rpicilli
    New Member
    • Aug 2008
    • 77

    #2
    Hi there,

    Certanly will have a better way but you can study this and modify. This is just for you get on the way. Put this code into a buttom_click event and go step by step to figured out what is going on

    Hope this help you

    Code:
            Dim i As Integer = 10
            Dim myArray(10) As String
            For u As Integer = 0 To i
                Dim jIn As String
                jIn = InputBox("Type Enter with no value to leave or Enter data: ")
                Try
                    If jIn = "" Then Exit Sub
                    Dim j As Integer
                    For j = 0 To i - 1
                        If myArray(j) = jIn Then
                            MessageBox.Show("Value is there")
                            u = 0
                            Exit Try
                        End If
                    Next
                    myArray(u) = jIn
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            Next

    Comment

    • jac130
      New Member
      • Oct 2008
      • 18

      #3
      I'm a bit confused by your code, and some of the terms you use.
      Dim j As Integer
      For j = 0 To i - 1
      If myArray(j) = jIn Then
      MessageBox.Show ("Value is there")
      u = 0
      Exit Try
      End If
      this is the portion of the code searching the array, right? I should also mention, that i think it has to go inside another for loop.

      Comment

      • rpicilli
        New Member
        • Aug 2008
        • 77

        #4
        Originally posted by jac130
        I'm a bit confused by your code, and some of the terms you use.
        Dim j As Integer
        For j = 0 To i - 1
        If myArray(j) = jIn Then
        MessageBox.Show ("Value is there")
        u = 0
        Exit Try
        End If
        this is the portion of the code searching the array, right? I should also mention, that i think it has to go inside another for loop.
        No problem. Try to understand now. If is still any problem just let me know.

        Code:
         Dim iMyNumberOfArrayItems As Integer = 10 
                Dim myArray(10) As String 
                For iLoopAllArray As Integer = 0 To iMyNumberOfArrayItems
                    Dim sUserEntry As String 
                    sUserEntry = InputBox("Type Enter with no value to leave or Enter data: ") 
                    Try 
                        If sUserEntry = "" Then Exit Sub 
                        Dim j As Integer 
                        For j = 0 To iMyNumberOfArrayItems - 1 
                            If myArray(j) = sUserEntry Then 
                                MessageBox.Show("Value is there") 
                                iLoopAllArray = 0 
                                Exit Try 
                            End If 
                        Next 
                        myArray(iLoopAllArray) = sUserEntry 
                    Catch ex As Exception 
                        MessageBox.Show(ex.Message) 
                    End Try 
                Next

        Good luck

        Comment

        Working...