1

I have to write a method that accepts an integer to look for and returns the position of the first occurrence found or -1 if not found in an array. This is what I have so far, however, it has an error when returning but I do not know what I am doing wrong:

public static int findValue (int [] z, int y) //y is the number given by the user that I need to find.
{
    for (int x = 0; x < z.length ; x++) 
    {
        if ( z[x] == y)
        {
            int w = x;
            break;
            return (w);
        }
    }
    else 
        return -1;

}

6
  • 1
    There cannot be a else clause for a for loop (in java)... Commented Dec 9, 2015 at 3:47
  • then what should I do to return -1 if "y" is not found? Commented Dec 9, 2015 at 3:47
  • Just remove the else and return... also why do you have a break before return inside if? Commented Dec 9, 2015 at 3:48
  • You have unreachable code below the break;. Commented Dec 9, 2015 at 3:49
  • I used break because I only need to return the first occurrence. If I do not break the loop isn't it going to keep going, and still return -1 if I do not use else? Sorry I'm a beginner Commented Dec 9, 2015 at 3:50

3 Answers 3

1

1.) else block is for if statement not for-loop statement.

2.) Also no need for break; statement, when you are returning from if

3.) Also no need for assignment, extra variable int w = x;, just return x is sufficient.

public static int findValue (int [] z, int y) //y is the number given by the user that I need to find.
    {
        for (int x = 0; x < z.length ; x++) 
        {
            if ( z[x] == y)
            {
              return x;
            }
        } 
          return -1;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You are really close to solving this:

  • Remove the else - You do not need an else after the loop, because the code after the loop is reached immediately after the loop exit condition is met.
  • Remove the break - return breaks the loop without an additional break statement.

That's it - the code is ready to go. As a point for style, you could remove declaration of w, and write return x for the entire body of your if statement. It's going to work as is, though.

Comments

1

You have a couple of problems with this code. First, you can't have a return right after a break statement; the return statement is unreachable. Just get rid of the break. EDIT (in response to a comment by OP): it's important to realize that when the return statement executes, it immediately terminates the loop (as well as the entire method). No other code in the method will run.*

Second, you can't have an else without a matching if, and here you don't need one. If you get that far, it's because the loop finished without executing a return—meaning that the value was not found. So just return -1.

Also, as a matter of style, you don't need the w variable; returning x will work just as well. Put it all together and the result might look like this:

public static int findValue (int [] z, int y)
{
    for (int x = 0; x < z.length ; x++) 
    {
        if ( z[x] == y)
        {
            return x;
        }
    }
    return -1;
}

*This is not always true. If a return executes inside a try or catch block that also has an associated finally block, then the code in the finally block will execute after the return statement finishes.

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.