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?
Integerrather thanint? What do you see inkeyand what were you expecting?