Dynamic Wordookie ( aka Dynamically Updating String Array with splitTokens)
in
Contributed Library Questions
•
2 years ago
Hello,
I know because of what I'm trying to do here, that this is to be filed under Contributed Library Questions, but I think that it's really a manifestation of my ongoing confusion when it comes to Strings and Arrays...
... so I think it's more of a simple Programming Question (just trying to avoid Phi.Lo's friendly rage here!
)...
My aim, essentially, is to create a string via keyboard input, assign it to a larger String Array, and split it at a carriage return.
I have a pared-down example of the basic problem here:
- String word = " "; // Starting with an empty string
- void setup() {
- size(400, 400);
- background(0);
- }
- void draw() {
- String [] Sentence = splitTokens(word, "\r");
- for(int i = 0; i < Sentence.length; i++) {
- println("S1 " + Sentence[i]);
- }
- }
- void keyTyped() {
- word += key; // add keystrokes to the string
- }
though its hard to tell if it's really working because I can't declare/print the various sub-arrays before they are created or else I get a NullPointerException...
Here is the larger code, adapted from one of the Wordookie examples...
- import wordookie.*;
- java.util.List wordList;
-
- static String FONT = "Serif";
- static final float maxFont = 100f;
- static final float minFont = 12f;
- static color [] textColors = {#F6EFF7, #BDC9E1, #67A9CF, #02818A};
- color bg = color(0);
-
- String word = " "; // Starting with an empty string
- String [] Sentence = splitTokens(word, "\r"); // Splitting sentences into a larger array
- int wordIndex = 0;
- PFont font;
- Layout layout;
-
- void setup() {
- size(400, 400);
- background(0);
- smooth();
- font = createFont(FONT, 48);
- this.loadWords(); // I realize this is only running once
- this.sortWords(); // Ditto
- layout = new Layout(this, bg);
- }
-
- void draw() {
- if(wordIndex < wordList.size() ) {
- Word word = (Word)wordList.get(wordIndex);
- word.font = font;
- layout.doLayout(word);
- fill(textColors[(int)random(textColors.length)]);
- layout.paintWord(word);
- wordIndex++;
- }
- else {
- //println("Done");
- //noLoop();
- }
- }
-
- void loadWords() {
- wordList = new ArrayList();
- for(int i = 0; i < Sentence.length; i++) {
- Word w = new Word(Sentence[i % Sentence.length], i);
- println("SENT " + Sentence[i]); // Returning 1
- wordList.add(w);
- }
- }
-
- void sortWords() {
- Collections.sort(wordList);
- float greatestWeight = ( (Word)wordList.get(0) ).weight;
- float leastWeight = ( (Word)wordList.get(wordList.size() -1)).weight;
- for(int i = 0; i < wordList.size(); i++) {
- Word w = (Word)wordList.get(i);
- w.fontSize = (int)map(w.weight, leastWeight, greatestWeight, minFont, maxFont);
- }
- }
-
- void keyTyped() {
- word += key;
- println("Word " + word);
- }
It seems like my Sentence[] is not being updated... it is only ever returning a length of 1 (as is wordIndex and wordList.size() )....
I've tried to move the loadWords() and sortWords() functions into draw() and/or keyTyped(), to see if this helps to recall the functions, but it doesn't visibly help.
I'm pretty sure that this is all an issue of me missing some functionality in ArrayList, String Arrays, or Strings... if it's obvious to anyone, I would appreciate any thoughts and leads...
Thanks,
~ J
2