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;
}
elseclause for aforloop (in java)...elseand return... also why do you have abreakbefore return inside if?break;.