Code not running properly - middle of word
<p>I'm writing a code to see if there's xyz in the middle of a given string.  This works if xyz appears once, but when it appears more than once, it won't always work.</p>

<pre><code>public boolean xyzMiddle(String str) {
  if (str.length() &lt;= 2) {
    return false;
  }
  int count1 = 0;
  int count2 = 0;
  for (int i=(str.length()-2)/2; i&lt;str.length()-2; i++) {
    if (str.substring(i, i+3).equals("xyz")) {
      count1 = str.substring(0, i).length();
      count2 = str.substring(i+3).length();
    }
  }
  if (count1 == count2 || count1+1 == count2 || count2+1 == count1) {
    return true;
  }
  return false;
}
</code></pre>
