0

I have a task in where I am instructed to create a method that returns words from a string based on a given length. The specific instructions are as follows:

This method will input a sentence with multiple words separated by spaces. The input number represents the size of the word we are looking for. Return a string array with all words found with the input size

Example:

String s = “Monday is a new day”;
int n = 3; //3 letter words
howManyWord(s, n) returns {“new”, “day”}
howManyWord(s, 2) returns {“is”}.

So far, this is my take at the solution. The main issue I am having is in the second for loop in terms of assigning words to the array itself

public String[] howManyWord(String s, int n) {
        //count the amount of words in the String
        int counter1 = 0;
        for(int a1 = 1; a1 < s.length(); a1++) {
          char c1 = s.charAt(a1-1);
          char c2 = s.charAt(a1);
          if(c1 != ' ' && c2 == ' '){
              counter1++;
          }
        }

        counter1 += 1;
        //Get the words of a string into an array + the loop in question
        String[] words = new String[counter1];
        String[] output = new String[counter1];
        for(int a2 = 1; a2 < s.length(); a2++) {
            char c1 = s.charAt(a2-1);
          char c2 = s.charAt(a2);
          int counter2 = 0;
          if(c1 != ' ' && c2 == ' '){
              int index1 = s.indexOf(c1);
              int index2 = s.indexOf(c2);
              words[counter2] = s.substring(index1, index2);
                  counter2++;
          }
        }

        //assign words of a specific length into output array
        for(int a3 = 0; a3 < output.length; a3++) {
           if(words[a3].length() == n){
               output[a3] = words[a3];
           }
        }
        return output;
        }

How would I go about this issue? Thanks!

1
  • How about just using String[] worlds = s.split(" ") and just checking the lengths? Commented Nov 5, 2022 at 22:42

2 Answers 2

2

Use split() to gets words from the sentence, stream them filtering for the length, then return the filtered words as an array:

public String[] findWords(String s, int wordLength) {
    return Arrays.stream(s.split(" "))
      .filter(word -> word.length() == wordLength)
      .toArray(String[]::new);
}

Also note the renaming of your method to findWords and the renaming of the parameter n to wordLength to be more descriptive; you're not returning how many (with return type int) but finding words with a certain length.

Sign up to request clarification or add additional context in comments.

Comments

0

Yor code is to complicate. use:

public static long howManyWord(String s, int n) {
    
    String[] words = s.split(" "); // split at blank to get all words
    return Arrays.stream(words).filter(s1 -> s1.length() == n).count(); // filter all words woth the length of n and count them
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.