String related problem from CodingBat
There's several 'special cases' in your code.


String of length 1
String of length 0
'g' at start of String
'g' at end of String


I'd recommend getting rid of these special cases.

To do this, you can check for special conditions inside the for-loop.

Loop through all the positions in the string, and check if there is a 'g' on that position. If it is, then check if the position is more than 0 (i.e. it has a char in front of it) or if the position is less than str.length - 1 (i.e. it has a char after it).

The details about how to write this can be done in oh so many ways, but here is what I would end up with:

for (int i = 0; i &lt; str.length(); i++) {
    char ch = str.charAt(i);
    if (ch == 'g') {
        if (i &gt; 0 &amp;&amp; str.charAt(i - 1) == ch) {
            continue;
        }
        if (i &lt; str.length() - 1 &amp;&amp; str.charAt(i + 1) == ch) {
            continue;
        }
        return false;
    }
}

return true;