Can anyone help me with this simple for loop, that just isn't working for me? If you would like to use the website that I'm doing this on, it is on javabat.com, String2 -> catDog. But for those who would rather not visit the site, please look at my code and tell me what I'm missing, because it really is driving me crazy.
Problem: Return true if the string "cat" and "dog" appear the same number of times in the given string.
My Code:
public boolean catDog(String str)
{ boolean result = false;
int cat = 0;
int dog = 0;
int len = str.length();
for(int i = 0; i <(len-2);i++)
{
String sub = str.substring(i, i+3);
if (sub.equals("cat")) cat ++;
if (sub.equals("dog")) dog ++;
if (cat != dog) result = false;
else result = true;
}
return result;
}
}
Any (useful) help is appreciated =)
Post edited at 12:38 pm on April 3, 2008 by xren
-------
N&R;D
you&me babe
October 10, 2004