1

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?

9
  • 5
    Is there any reason you are using Integer rather than int ? What do you see in key and what were you expecting? Commented Aug 22, 2012 at 7:59
  • 5
    You're using == for object comparison (which does reference comparison). You should use equals, or simple int. This code will only accidentally work when your numbers are in the Integer cache range Commented Aug 22, 2012 at 7:59
  • Use equals instead of == to compare integers. Or use int instead. Commented Aug 22, 2012 at 7:59
  • you are comparing Integer objects by reference. You need to use equals Commented Aug 22, 2012 at 7:59
  • This code is working what is the wrong in here???? Commented Aug 22, 2012 at 8:14

8 Answers 8

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

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

Comments

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

or

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

Comments

0

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.

Comments

0

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,

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.