By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

How to search in array?

P: 43

I want to search number in array, i wrote the following code but it doesnt work
please check it
and sned me the solution.
Expand|Select|Wrap|Line Numbers
  1. Sub Main()
  2.         Dim array As Integer() = New Integer(3) {}
  3.         Dim search As Integer
  4.         For i As Integer = 0 To array.GetUpperBound(0)
  5.             Console.Write("Enter any number for array location [{0}]: ", i + 1)
  6.             array(i) = Console.ReadLine()
  7.         Next
  8.         Console.WriteLine()
  9.         For i As Integer = 0 To array.GetUpperBound(0)
  10.             Console.WriteLine("The outpu of array is {0}", array(i))
  11.         Next
  12.         Console.WriteLine()
  13.         Console.Write("Enter any value to search: ")
  14.         search = Console.ReadLine()
  15.         For i As Integer = 0 To array.GetUpperBound(0)
  16.             If array(i) = search Then
  17.                 Console.Write("The search value found at array location {0}", i)
  18.                 Exit For
  19.             End If
  21.             If array(i) <> search Then
  22.                 Console.Write("the search does not matched with array")
  24.             End If
  25.         Next
  26.         Console.ReadLine()
  27.     End Sub
thanks in advance
Apr 15 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 2.5K+
P: 3,525
TIP: When you first created your question you were asked to wrap your code with [code] tags.

It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Apr 15 '10 #2

Expert 2.5K+
P: 3,525
"It doesn't work" is hardly helpful. Could you be a little more specific as to the problem? You get a wrong value... you get an exception error...

May I suggest you put a breakpoint at line 15... then walk through the loop line by line with the F10 key. Look at the values of your variables in the 'Locals' or 'Autos' pallet. You'll probably find that the match is failing because 15 does not equal "15\r" or something similar
Apr 15 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.