1

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");
        }
    }
}
11
  • so what's your question? Commented Jul 28, 2016 at 10:32
  • Your solution looks fine.are you facing any issue? Commented Jul 28, 2016 at 10:33
  • This code is quiet good and it's the way you are supposed to write it, what's the point ? Commented Jul 28, 2016 at 10:33
  • You don't seem to know what problem you have, or even if you have a problem to be solved. Commented Jul 28, 2016 at 10:33
  • 4
    The method findChar(Char character, String word) signature is wrong, it should be findChar(Char character, String[] words) Commented Jul 28, 2016 at 10:33

1 Answer 1

3

This should work

public static void findChar(char character, String words[]) {
    //ArrayList for putting the matched words
    ArrayList<String> wordsContainingCharacter = new ArrayList<String>();
    for (String word : words) {
        // Check if it contains the character
        if (word.contains(Character.toString(character))) {
            wordsContainingCharacter.add(word);
        }

    }
    //2.check is to be put after the loop
    if (wordsContainingCharacter.size() > 0) {
        System.out.println("Words containing the letter " + character
                + " : " + wordsContainingCharacter);
    } else {
        System.out.println("Character is not in any word");
    }
}

Logic without using the ArrayList

    StringBuilder stringOfWords=new StringBuilder();
    for (String word : words) {
        // Check if it contains the character
        if (word.contains(Character.toString(character))) {
            stringOfWords.append(word+" ");
        }

    }
    //2.check is to be put after the loop
    if (stringOfWords.length() > 0) {
        System.out.println("Words containing the letter " + character
                + " : " + stringOfWords);
    } else {
        System.out.println("Character is not in any word");
    }
Sign up to request clarification or add additional context in comments.

7 Comments

is there a way to do without using ArrayList<string> I havn't learnt that before
Yes you can use simple String
how would you do that ?
@Logesh im getting this error >>> Main.java:4: illegal start of type for (String word : words) {
@ross.c you need to correct the method parameter types
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.