I need write a method called "occurences" that takes a string and a list of words as parameter that returns how many times a certain String in that list occurs. It needs to ignore upper and lowercase and also that parts of the string may also count as occurence, for example:
i wanna check the string "Dogregister" in my list of [cat, dog, ister], the program is supposed to return 2, because "dog" and "ister" is both in the string "Dogregister".
So i have started with making the arraylist with the words and now i wanna implement the method that can return how many occurences there are in the list. i am not sure how to implement the list as a parameter in my method? do i write ArrayList words = new ArrayList<>(); ,
also i havent figured out how to count occurence of parts of a string yet, im trying to find a string method that can help me achieve what i want.
You can use Regex.
List<String> words = new ArrayList<>();
words.add("katt");
words.add("hund");
words.add("ister");
String word = "Hundregister";
int count = 0;
for (int i = 0; i < words. Size(); i++) {
String current = words.get(i);
boolean occurrence = occurrence(current, word.toLowerCase(Locale.ROOT));
if (occurrence) {
count++;
}
}
System.out.println(count);
private static boolean occurrence(String searchedWord, String word) {
Pattern pattern = Pattern.compile(searchedWord);
Matcher matcher = pattern.matcher(word);
return matcher.find();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.