0
public static String[] findWordsOfLength(String letters, int wordSize) {
    letters = "fourgooddogsswam";
    for(int i = 0; i < letters.length()-3;i++){
        String fourLetter = letters.substring(i,i+4);
    }
   for(int i = 0; i < letters.length()-4;i++){
        String fiveLetter = letters.substring(i,i+5);
    }

    return ?;
}

I was wondering if I could somehow set the list of strings generated from this loop, into a string array? Is there a way I could return that value if I used multiple loops for different strings?

Aimee Borda
  • 842
  • 2
  • 11
  • 22
Billy
  • 19
  • 4

2 Answers2

0

As Scary Wombat suggested in the comments, in the beginning of the method, you can create an ArrayList. Then within in each loop iteration, you can just keep adding to it. At the end of the method, you can convert the list to an array using the toArray method.

import java.util.ArrayList;
import java.util.List;

class Example {

    public static String[] findWordsOfLength(String letters, int wordSize) {
        if(letters == null) {
            return null;
        }

        int size = letters.length();
        int wordMax = size - wordSize + 1;
        if(size < wordMax || wordMax <= 0) {
            return new String[0];
        }

        List<String> result = new ArrayList<>();

        for (int i = 0; i < wordMax; i++) {
            result.add(letters.substring(i, i + wordSize));
        }

        return result.toArray(new String[0]);
    }

    public static void main(String[] args) {
        String[] word = findWordsOfLength("fourgooddogsswam", 4);

        for(String word : words) {
            System.out.println(word);
        }

        /**
        * Output in console:
        *
        * four
        * ourg
        * urgo
        * rgoo
        * good
        * oodd
        * oddo
        * ddog
        * dogs
        * ogss
        * gssw
        * sswa
        * swam
        */
    }
}

Note that the advantage of using a list is that you don't need to know its size in advance. An array, on other hand, is initialized with a set size.

If you must use an array, you could do:

public static String[] findWordsOfLength(String letters, int wordSize) {
    if(letters == null) {
        return null;
    }

    int size = letters.length();
    int wordMax = size - wordSize + 1;
    if(size < wordMax || wordMax <= 0) {
        return new String[0];
    }

    int j = 0;
    String[] result = new String[wordMax];

    for (int i = 0; i < wordMax; i++) {
        result[j ++] = letters.substring(i, i + wordSize);
    }

    return result;
}
Community
  • 1
  • 1
Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
  • I hate to ask but could you be more specefic? How would I use an ArrayList in this circumstance? thanks – Billy Mar 15 '17 at 06:40
  • Is there a way do do it without using array list? – Billy Mar 15 '17 at 06:45
  • @Billy The advantage of a list is that it will grow as you add items to it. An array, on other hand, has a definitive size, determined when initialized. Although, in this particular case, it seems you would know the size of array. – Moishe Lipsker Mar 15 '17 at 06:47
  • Yes but I'm not allowed to use ArrayLists, how else would I be able to do this? – Billy Mar 15 '17 at 06:50
  • @Billy I've added a second solution using just an array. – Moishe Lipsker Mar 15 '17 at 06:55
  • Is there a way I can set the fourgooddogsswam so that it shifts right by one character and sets the four characters to an array? So the result would be:four ourg urgo rgoo good oodd oddo ddog dogs ogss gssw sswa swam – Billy Mar 15 '17 at 22:29
  • @Billy yes just call the `findWordsOfLength` method with whatever string and word count you want. I've updated the answer to use your example. You can also run it online [here](https://ideone.com/f8vOCR). – Moishe Lipsker Mar 16 '17 at 03:11
0

Here´s an answer not involving a List.

Based on the input you can precalculate the output length of your array and initialize it with the given size. Than you´d just be looping over the array size and use mathematics again to substring from the input.

public static void main(String[] args){
    String[] words = findWordsOfLength("fourgooddogsswam", 4);
    print(words);
}

public static String[] findWordsOfLength(String letters, int wordSize) {
    // your working and output array definition
    String[] words;
    // If not even dividable add one to the size
    if (letters.length() % wordSize == 0) {
        words = new String[letters.length() / wordSize];
    } else {
        words = new String[letters.length() / wordSize + 1];
    }
    for(int i = 0; i < words.length;i++){
        // To not run into IndexOutOfBound check if we´d be going out of bound
        if(i*wordSize + wordSize > letters.length()) {
            words[i] = letters.substring(i*wordSize);
        } else {
            words[i] = letters.substring(i*wordSize, i*wordSize+wordSize);
        }
    }
    return words;
} 

// Just prints
public static void print(String[] input) {
    for(String string : input) {
        System.out.println(string);
    }
}

O/P:

four
good
dogs
swam
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33