Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

The Question: Create a static method called findElementIndex, which takes an integer and an integer array as input parameters, and returns the smallest index where that value appears. If the value is not in the array, the method should return -1. Example: value: 3 theArray:{10, 3, 6, 3, 8, 10} ==> 1 or value: 10 theArray:{11, 3, 6, 7, 9, 60} ==> -1

public static int findElementIndex (int value, int [ ] theArray) 
{  } 

My code: Will not compile & I think i've looked at it too many times to notice a mistake.

int index = 0;
int n = -1;

for(int i =0; i < theArray.length; i++) 
    if(value == theArray[i]) {
        index = i; 
        return index; 
    }
    else {
        return n;
    }

Thank you.

share|improve this question
1  
Step 1: Format your code readably. Step 2: Look closely at the helpful error the compiler is giving you. – T.J. Crowder Mar 11 '15 at 18:06
    
What compile errors are you receiving? – tnw Mar 11 '15 at 18:06
    
You forgot to put braces around the body of your for-loop – alfasin Mar 11 '15 at 18:07
1  
@alfasin: Technically, he/she doesn't need them. I'd put them there, though. – T.J. Crowder Mar 11 '15 at 18:09
1  
@Minty: You changed the code, you didn't just fix the formatting. It's not appropriate to change the code in the question unless you're doing something like moving content from a comment from the OP into the question. – T.J. Crowder Mar 11 '15 at 18:14

Make sure you always format your code nicely. Your intention is to return -1 if the check within the for loop is never triggered on any iteration, but you're checking if it's not triggered just on the first iteration. The program flow becomes more apparent when you add proper braces to your code:

for(int i = 0; i < theArray.length; i++) {
  if (value == theArray[i]) {
    return i; 
  }
}
return -1; // else return -1

I've also removed the extra variables - you can just return i and -1 directly.

share|improve this answer

I put your code into Eclipse, and the compiler complains that the method must return a result of type int.

What the compiler says is wrong is that if your array is passed in with 0 length, your for loop will never go into the if statements, so you need an extra return statement outside the for loop.

The other logical error that your compiler won't complain about is that you used an else statement. That means that if the array is more than length 0, after the first test you will instantly quit the function and return either 0 or -1.

eg:

public static int findElementIndex(int value, int[] theArray) {
    int index = 0;
    int n = -1;
    for (int i = 0; i < theArray.length; i++) {
        if (value == theArray[i]) {
            index = i;
            return index;
        }
    }
    return -1; // if the array was length 0, your for loop will not run.
}

Also, please use proper brackets.

Not having brackets is a very stupid idea.

You can accidentally write code like this, where you want to run the 3 do this lines inside the for loop:

for (condition)
    do this
    do this 2
    do this 3

but would actually mean

for (condition) { 
    do this 
}
do this 2
do this 3

which leaves out do this 2 and do this 3 - not what you were expecting. It's a pain to debug without brackets as well. If only you put the brackets in on the first shot.. you wouldn't even come to that problem.

share|improve this answer

You have some odd code formatting, but you also are missing a key step - you only want to return -1 if you've finished going through all the values and didn't find it! Also, you don't need to store values in a variable just to immediately return them!

Try this:

int index = 0;
for(int i =0; i < theArray.length; i++){
    if(value == theArray[i]) {
        return i; //returns where we found it if it matches
    }
}
return -1; //this line is only reached if we didn't already return
share|improve this answer
3  
index will always be 0 – Shang Mar 11 '15 at 18:08
    
@Shang haha good point! Thanks for pointing that out! – childofsoong Mar 11 '15 at 18:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.