# How do you find the the exact index of an element that matches a search in an array?

• August 21st, 2013, 03:42 PM
ojonugwa
How do you find the the exact index of an element that matches a search in an array?
Code :

```import java.util.*; public class ArrayTest { public static void main (String[]args) { Scanner input = new Scanner(System.in); int [] number = new int[5];   System.out.println("Enter 5 numbers"); for (int i=0; i<number.length;i++) { number[i] = input.nextInt();   } } }```
lets say i enter 4,6,70,12,5
I want to search for 70 (using a loop of course), and when i find it, i want to display the index it is in.in this case 2
• August 21st, 2013, 04:49 PM
Norm
Re: How do you find the the exact index of an element that matches a search in an array?
• August 21st, 2013, 09:02 PM
Junky
Re: How do you find the the exact index of an element that matches a search in an array?
Iterate over the array and compare each element to the target. When you get a match you have found the index. This is best achieved using a method that can return the index as soon as it is found or some other default value (-1) if it is not found.
• August 22nd, 2013, 12:40 AM
ojonugwa
Re: How do you find the the exact index of an element that matches a search in an array?
This is what i eventually did
Code :

```import java.util.*; public class ArrayTest { public static void main (String[]args) { Scanner input = new Scanner(System.in); int [] number = new int[5]; int index =0; int numToSearchFor = 50; System.out.println("Enter 5 numbers"); for (int i=0; i<number.length;i++) { number[i] = input.nextInt();   } for (int i=0; i<number.length;i++) { ++index; if (number[i] == numToSearchFor) { index = index-1; System.out.println("Found it at index " + index); }       } } }```
Works fine, but i was hoping there was a function in the java API that did this.
• August 22nd, 2013, 08:17 PM
Junky
Re: How do you find the the exact index of an element that matches a search in an array?
Are you expecting the creators of java to anticipate every single function that you can think of and write it for you? Just so when you write your own program it will be a series of method calls to API classes and you never have to write your own code every again!
• August 22nd, 2013, 10:17 PM
ojonugwa
Re: How do you find the the exact index of an element that matches a search in an array?
Quote:

Originally Posted by Junky
Are you expecting the creators of java to anticipate every single function that you can think of and write it for you? Just so when you write your own program it will be a series of method calls to API classes and you never have to write your own code every again!

That was VERY helpful
• August 22nd, 2013, 11:18 PM
jps
Re: How do you find the the exact index of an element that matches a search in an array?
Quote:

Originally Posted by ojonugwa
This is what i eventually did
Code :

```... if (number[i] == numToSearchFor) { // Then i is the correct index number, just print out i // The variable "index" is unnecessary```
i was hoping there was a function in the java API that did this.

Perhaps there is, did you look? Check out this page and see what you can find
• August 23rd, 2013, 12:04 AM
ojonugwa
Re: How do you find the the exact index of an element that matches a search in an array?
Code :

```import java.util.*;   public class ArrayTest2 {   public static void main(String[] args) {   Scanner input = new Scanner(System.in); int[] numbers = {8, 11, 19, 84};     int numToSearchFor; int i;   for (i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + ","); }   System.out.println("\nEnter the number to search for: "); numToSearchFor = input.nextInt();     for (i = 0; i < numbers.length; i++) { if (numbers[i] == numToSearchFor) { System.out.println("\nFound "+ numToSearchFor + " at index " + i);   } }}}```
This works perfectly, until i add an "else" statement like this
Code :

``` if (numbers[i] == numToSearchFor) { System.out.println("\nFound "+ numToSearchFor + " at index " + i);break;   } else { System.out.println("Cannot find it");break; }```
Then it starts to print "cannot find it"
Why is that?
• August 23rd, 2013, 01:53 AM
jps
Re: How do you find the the exact index of an element that matches a search in an array?
Quote:

Originally Posted by ojonugwa
Then it starts to print "cannot find it"
Why is that?

That may be because you search one element of the array at a time. When the first index(0) is searched, the if statement is encountered. If there is no match the else executes, the break exits the loop and no other index is considered.
Follow the loop cycle as it would be executed, you should see how it runs the way described.
• August 23rd, 2013, 10:52 AM
ojonugwa
Re: How do you find the the exact index of an element that matches a search in an array?
Finally got help from my tutor.This is one possible way to do it.
Code :

```import java.util.*; public class ArrayTest{ public static void main (String[]args){ Scanner input = new Scanner(System.in); int [] number = new int[5]; int numberToSearchFor; int i; boolean found=false; for (i=0; i<number.length;i++){ number [i] = (int)(Math.random()*20)+1; System.out.println(number[i]); } System.out.println("Enter number to find from available numbers: "); numberToSearchFor = input.nextInt(); i=0; while(!found&&i<number.length) if (number[i] == numberToSearchFor){ System.out.println("Found it at index " + i); found=true; } else i++; if(!found) System.out.println("Cannot find it"); } }```