Hello,
I’m working on a sketch where at some point I want to load a 2D String array with randomly generated ‘words’ (which are randomly generated strings of chars). I want to pass these on to an Object and reference and use them later in the sketch. After initializing and loading the 2D array I noticed that, when trying to reference Strings in the array, it would return a null for every given entry.
I managed to isolate the problem, currently the code that’s not working the way I intended it to looks like this:
String wordList[][];
int iMax, jMax;
void setup() {
size(500, 500);
iMax = 50;
jMax = 20;
for (int i = 0; i < iMax; i++) {
for (int j = 0; j < jMax; j++) {
String rWord = getAlphaString(20);
wordList = new String[iMax][jMax];
println(rWord);
wordList[i][j] = rWord;
println(rWord);
println(wordList[i][j]);
}
}
println(wordList.length, wordList[0].length, wordList[0][10]);
}
void draw(){
//wordList[0][0] = getAlphaString(10);
//println(wordList[0][0]);
}
// function to generate a random string of length n
String getAlphaString(int n) {
String AlphaString = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; i++) {
int index = (int)(AlphaString.length() * Math.random());
sb.append(AlphaString.charAt(index));
}
return sb.toString();
}
I’m not sure why this method in Setup seemingly doesn’t store the 2D array with a set of String data. When uncommenting the lines in Draw it seems to work out the way I intend it to.
Am I overseeing something? Am I loading the 2D-array in setup in an incorrect way? Is the function used to generate the String of text not suited for my goal? Any input is welcome, thanks a lot in advance with helping me!
Namrad