Im a beginner at java and I've been assigned this exercise I've been working on it for a while now but I just cant seem to get it right.
Return true if the string "cat" and "dog" appear the same number of times in the given string.
this is my code so far.
public boolean catDog(String str)
{
int count = 0;
for (int i=0; i<str.length(); i++)
{
String sub = str.substring(i, i+1);
if (sub.equals("cat"))
count++;
if (sub.equals("dog"))
count--;
}
{return false;}
}
any hints on how to fix it?
http://www.javabat.com/prob?id=String2.catDog