Q:

Java, return true if 'x' is next to 'y' in an array

This is, I imagine, a really simple problem to solve however I just can't figure it out. 
An array contains a list of integers and I want to return true if every number 'x' in the array is followed by the number 'y'.
So arrays with {x,3,4,y} or {x,x,y,4,5} or {5,8,x,x} would be false.
Whereas arrays with {x,y,4,1} or {x,y,5,1,x,y} would be true.
This is what I have tried so far:
for (int i = 0; i < nums.length-1; i++)
{
    if (nums[i] == x && nums[i+1] == y)
    {
        return true;
    }
    else
    {
        return false;
    }
}

return false;

My code however will only work for the first two elements in the array (so 0 and 1). It won't detect any integers further down in the array, so how do I do this?
Thank you.

A:

I want to return true if every number 'x' in the array is followed by the number 'y'.

You need to get rid of the else and modify the checks like so:
for (int i = 0; i < nums.length - 1; i++)
{
    if (nums[i] == x && nums[i + 1] != y)
    {
        return false;
    }
}

return true;

Caveats:

This returns true if x is not present in the array. It's unclear from the question whether this is the behaviour you want.
This does not check whether x is the last element of the array. Again, it's not entirely clear what you'd expect to happen if it is.

