Question: I am working on a program where I take input from the user and I the enter 24 length `String` with numbers 0-5 and each number is repeated 4 times in the line. An example would be like this `000011112222333344445555`. I realized when I am collecting the numbers for the user that I can't have them in a `2d int array of [6][4]`. Which is what I want so what I want to do is take that string and put the each of the numbers in that array. So it would look like this `[[0,0,0,0],[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4],[5,5,5,5]]`. I am not sure how to get them into an array like this. I know that strings are immutable so I can't change the string but I did think I could take the string and turn it into `char` array and then insert each value into my desired int array. I don't know how to do this any help or guidance on how to approach this would be really appreciated. Thanks in advance

Answer: 
Tested on <https://www.compilejava.net/> Also works if you have something different than:

> 
> I the enter 24 length String with numbers 0-5 and each number is
>  repeated 4 times in the line
> 
> 
> 

```
import java.util.ArrayList;
public class HelloWorld
{

  public static void main(String[] args)
  {
    String s = "00001111222233334444555599";
    ArrayList<ArrayList<Integer>> collection = new ArrayList<ArrayList<Integer>>();
    for (int i = 0; i <= 9; i++){
        collection.add(new ArrayList<Integer>());
    }

    for (int i = 0; i < s.length(); i++){
        int c = Character.getNumericValue(s.charAt(i));        
        collection.get(c).add(c);
    }

    for (int i = 0; i <= 9; i++){
        System.out.println(collection.get(i));
    }

    System.out.println(collection);
  }
}

```

Results:

```
[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
[4, 4, 4, 4]
[5, 5, 5, 5]
[]
[]
[]
[9, 9]
[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5], [], [], [], [9, 9]]

```
So you know your string must have 4 repeating elements, I assume after 4 repeats, a new value follows. So to me it seems like an N x 4 array. 

Thus you can do, and I will do pseudocode for now to help guide you. 

int[][] myArray = new int[str.length/4][4]

We divide by four since we know if we have "000011112222" our total length is 12. 12/4 is 3 and we would expect 3 columns. 4 is the amount of columns we have, and we know it will be only 4. 

After that, and in here I assume you are entering the values one after the other, without any delimiter. We can do:

```
    int col = 0;
    int row = 0;
for(i from 0 to string length) {
    if((I+1) % 4 != 0) {
       //keep adding to myArray[row][col]
       //row++
      }
    } else { // so we now hit new numbers
       col++;
       row = 0;
      }
}

```

So in here I tried to stick to only one for-loop. I was thinking of doing it with a double for-loop, but wasn't sure on how I would approach it in this case since we are dealing with an empty array initially. 

Quick edit, one thing you could do, btw. Is make a 2D List as so: 

```
List<List<String>> strings = new ArrayList<List<String>>()

int index = 0;
while (index < text.length()) {
    String pattern = text.substring(index, Math.min(index + 4,text.length()));
   List<String> splitPattern = Arrays.asList(pattern.split(""));
   strings.add(splitPattern);
    index += 4;
}

```

So the logic here is we make a substring, split it on "" so nothing in otherwords it will just produce 0, 0, 0, 0 as the delimiter is nothing. and then we add it to our list of lists, and you would access by saying strings.get(I).get(j);