I'm trying to write a method findChar(). This method takes a character and an array of strings as parameters and it prints all the words in the array which contain the specified parameter character. for example
Test Result
String[] words = {"caravan", "car", "van", "bike", "scooter", "vehicle", "bicycle"};
findChar('v', words); Words containing the letter v : caravan van vehicle
findChar('i', words); Words containing the letter i : bike vehicle bicycle
I got something like this at the moment it's a rough idea but not 100% sure as I'm still a week into learning java so please go easy.
public static void findChar(Char character, String word) {
for(String word:words) {
// Check if it contains the character
if (word.contains(Character.toString(character))) {
wordsContainingCharacter.add(word);
}
if(.....size() > 0) {
System.out.println("Words containing the letter " + character + " : " + ....);
} else {
System.out.println("Character is not in any word");
}
}
}
findChar(Char character, String word)signature is wrong, it should befindChar(Char character, String[] words)