java - find character in array -


i'm trying write method findchar(). method takes character , array of strings parameters , prints words in array contain specified parameter character. example

test                                 result string[] words = {"caravan", "car", "van", "bike", "scooter", "vehicle", "bicycle"};  findchar('v', words);  words containing letter v : caravan van vehicle findchar('i', words);  words containing letter : bike vehicle bicycle 

i got @ moment it's rough idea not 100% sure i'm still week learning java please go easy.

   public static void findchar(char character, string word) {    for(string word:words) {             // check if contains character             if (word.contains(character.tostring(character))) {                 wordscontainingcharacter.add(word);             }      if(.....size() > 0) {             system.out.println("words containing letter " + character + " : " + ....);         } else {             system.out.println("character not in word");         }     } } 

this should work

public static void findchar(char character, string words[]) {     //arraylist putting matched words     arraylist<string> wordscontainingcharacter = new arraylist<string>();     (string word : words) {         // check if contains character         if (word.contains(character.tostring(character))) {             wordscontainingcharacter.add(word);         }      }     //2.check put after loop     if (wordscontainingcharacter.size() > 0) {         system.out.println("words containing letter " + character                 + " : " + wordscontainingcharacter);     } else {         system.out.println("character not in word");     } } 

logic without using arraylist

    stringbuilder stringofwords=new stringbuilder();     (string word : words) {         // check if contains character         if (word.contains(character.tostring(character))) {             stringofwords.append(word+" ");         }      }     //2.check put after loop     if (stringofwords.length() > 0) {         system.out.println("words containing letter " + character                 + " : " + stringofwords);     } else {         system.out.println("character not in word");     } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -