Random Word Picker

So I just want to create a simple random word picker that can display one word every time the program is run.

So far I have this code but it keeps generating words after the first one has popped up, and currently there is an issue with the String command. I just can’t seem to fix it. Could anyone help? I would really appreciate it.

Here’s the code:

//String text;
String words[];
String word;
PFont font;

void setup() {
    size(500,500);
    // Join does the opposite of split.
    // Give it an array of strings and it will join them
    // together using the character provided.
    // loadStrings returns a String array from a text file.
    // Put the file in your sketches data folder.
    String text = loadStrings("list.rtf");
    String[] words = split(text, " ");
   

    // Draw once a second.
    frameRate(1);
}

void draw() {
    int index = int(random(words.length));
    word = words[index];
    // Display word in the centre of the screen.
    font = loadFont("GraphikArabic-Semibold-48.vlw");
  textFont(font, 60);
    textAlign(CENTER);
    text(word, width/2, height/2);
    println( words.length);
}

Here’s a link to download list.rtf:

1 Like

could you post "list.rtf" too?

1 Like

draw runs 60 times per second

So these lines above belong into setup

LoadFonts also into setup, time consuming for the processor

4 Likes

try to replace these two line

String text = loadStrings("list.rtf");
String[] words = split(text, " ");

with just this
words = loadStrings("list.rtf");

is it working?

3 Likes

-a- a RTF file contains formatting info, cannot be used here,
pls. copy content to list.txt file

-b- your file has lines with one word ( can not split on " " )
and lines with 2 or more words, so need to split those ones??

-c- an example code could be

// can not use RTF file and read string lines, copy content to list.txt
String infile = "data/list.txt";
String[] lines;    // from file, may contain one word or more words separated with " "
String[] words = {""};
//PFont font;
int index;

void setup() {
  size(500, 500);
  lines = loadStrings(infile);
  for (String line : lines ) {
    // println(line);
    trim(line);                                             // clean ends
    String[] linewords = {""};                              // init needed
    String[] m = match(line, " ");
    if (m != null)   linewords = splitTokens(line, " ");    // make array from line
    else             linewords[0]=line;                     // only one word in that line, no " "
    words = concat(linewords, words);                       // add to all words array
  }
  println(words);
  println( words.length);

  frameRate(1);   // Draw once a second.
}

void draw() {
  background(200, 200, 0);
  //  font = loadFont("GraphikArabic-Semibold-48.vlw");
  //  textFont(font, 60);
  textSize(60);
  textAlign(CENTER);
  index = int(random(words.length));
  text(words[index], width/2, height/2);    // Display word in the centre of the screen.
}

2 Likes

Added a download link!

sorry, what you edit / add where / and what you link to?

-a- did you change your RTF file to a TXT file?
-b- did you test my example?

1 Like

So sorry for the super late reply, but this worked perfectly! Thank you so much.

1 Like