To count number of ocurrences of a String in another String create a function (extracted from here):
The "split and count" method:
public class CountSubstring {
public static int countSubstring(String subStr, String str){
// the result of split() will contain one more element than the delimiter
// the "-1" second argument makes it not discard trailing empty strings
return str.split(Pattern.quote(subStr), -1).length - 1;
}
The "remove and count the difference" method:
public static int countSubstring(String subStr, String str){
return (str.length() - str.replace(subStr, "").length()) / subStr.length();
}
Then you just have to compare:
return countSubstring("dog", phrase) > countSubstring("cat", phrase);
ADDITIONAL INFORMATION
To compare strings use String::equals or String::equalsIgnoreCase if you don't mean uppercase
and lowercase
.
string1.equals(string2);
string1.equalsIgnoreCase(string2);
To find number of ocurrences of a string in another string use indexOf
string1.indexOf(string2, index);
To see if a string contains another string use contains
string1.contains(string2);