JackyBoi
JackyBoi

Reputation: 2773

Java return index if value found in array

I am trying to find a value in an array and I know for sure that this value will exist only once, so i am trying to find the value and return the index of the array where it is stored, if not found return -1 This is what I am trying to do:

static Integer[] accs = new Integer[20];
 public static int search()
    {
        Integer[] numbers;
        numbers = accs;
        Integer key;
        Scanner sc = new Scanner(System.in);
         System.out.println("Enter the Account Number:");        
         key = sc.nextInt();

        for (Integer index = 0; index < numbers.length; index++)
      {
           if ( numbers[index] == key )
                 return index;  //We found it!!!
      }
     // If we get to the end of the loop, a value has not yet
     // been returned.  We did not find the key in this array.
     return -1;

    }

When I run this even though I know that the value exists in the array, nothing is being displayed. I debugged and then I found out that the Key variable is not having the value of what I typed. Is something wrong there?

Upvotes: 1

Views: 35421

Answers (8)

jolivier
jolivier

Reputation: 7635

Your array is storing Integer, which is a java Object, to test for equality of Java Object you need to call Object.equals method instead of ==.

In your case I suggest you use an array of int instead of Integer, it is lighter in memory, support == and you are not using any of the features of Integer so you don't need them here.

Upvotes: 7

Use following command for capture user input BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Upvotes: 0

mssb
mssb

Reputation: 878

initialize the array like this way static Integer[] accs = {3,5,15,6,4,32,9}; and call search() method in the main. your function work properly.

Upvotes: 0

user1616299
user1616299

Reputation: 1

You need set value for each element before search. The statement below only declare new array with 20 element with value = null static Integer[] accs = new Integer[20]; Replace by: static Integer[] accs = new Integer[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; and retest.

Regards,

Upvotes: 0

Ilya
Ilya

Reputation: 29693

java.util.Arrays.asList(accs).indexOf(key)  

or

org.apache.commons.lang.ArrayUtils.indexOf(accs, key);

Upvotes: 1

Andreas Fester
Andreas Fester

Reputation: 36630

Make sure you understand the difference between object equality and object identity. You are using "==" to check if your value is in the array - this checks for identity, not for equality. Use numbers[index].equals(key) instead.

Upvotes: 0

amit
amit

Reputation: 178451

You are checking for identity when checking with operator== (Are the two references point to the exact same object?) and not equality (do they equal each other?).

You should use ints (rather then Integers) or change the condition to use the equals() method.

Upvotes: 4

Jaco Van Niekerk
Jaco Van Niekerk

Reputation: 4182

Integer is an object, so you have to make use of the equals method. Try numbers[index].equals(key).

Upvotes: 2

Related Questions