My goal, in Java, is to return true of the number of occurrences of "cat" in a string is equal to the number of occurrences of "dog".
public boolean catDog(String str) {
int matches = 0, matches2 = 0;
for(int i = 0; i < str.length()-2; ++i)
{
if(str.substring(i,i+3)=="cat")++matches;
if(str.substring(i,i+3)=="dog")++matches2;
}
if(matches==matches2)return true;
return false;
}
It always returns true(probably not incrementing counters) unless str is exactly "dog" or "cat". Why?