Q:

Syntax error on token "return", invalid Type?

I'm trying to complete this exercise from codingbat.com in Java. I get this error "Syntax error on token "return", invalid Type" on this little bit of code and I'm having trouble figuring out why. I'm trying to return the number of times the word "hi" appears in a given String. Thanks!
public int countHi(String str) {
    int count = 0;
    for(int i = 0; i < str.length() - 1; i++){
        if(str.substring(i, i + 2).equals("hi"));
            count++;
        }
    }
    return count;
}

A:

public int countHi(String str) {
    int count = 0;
    for(int i = 0; i < str.length() - 1; i++){
        if(str.substring(i, i + 2).equals("hi")); // 1
            count++;
        } // 2
    }
    return count;
}

The issue is that you have an ; rather than a { after your if condition (1), which essentially means the if body is empty. The } after the count++ line (2) is, in turn, treated as the end of the for loop (rather than the if as it should be), and the } that should end the for loop instead ends the method.
That leaves your return count; and final } hanging in the middle of a class definition where it's not valid syntax.

