# Random Word Picker

**URL:** <https://discourse.processing.org/t/random-word-picker/10619>\
**Category:** Coding Questions\
**Created:** [April 25, 2019, 6:47am UTC](https://discourse.processing.org/t/random-word-picker/10619 "2019-04-25T06:47:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariamalz](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@mariamalz](https://discourse.processing.org/u/mariamalz)\
**Post date:** [April 25, 2019, 6:47am UTC](https://discourse.processing.org/t/random-word-picker/10619/1 "2019-04-25T06:47:03Z")

</div>

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:

```auto
//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:

> **[list.rtf](https://wetransfer.com/downloads/136defb9a6c56ca84c9b062b2391709a20190425070633/e1e857)**
>
> 1 file sent via WeTransfer, the simplest way to send your files around the world

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [April 25, 2019, 6:55am UTC](https://discourse.processing.org/t/random-word-picker/10619/2 "2019-04-25T06:55:54Z")

</div>

could you post `"list.rtf"` too?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 25, 2019, 7:32am UTC](https://discourse.processing.org/t/random-word-picker/10619/3 "2019-04-25T07:32:13Z")

</div>

> [@mariamalz](#):
>
> int index = int(random(words.length)); word = words[index];

draw runs 60 times per second

So these lines above belong into setup

LoadFonts also into setup, time consuming for the processor

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [April 25, 2019, 7:49am UTC](https://discourse.processing.org/t/random-word-picker/10619/4 "2019-04-25T07:49:23Z")

</div>

try to replace these two line

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

```

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

is it working?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 25, 2019, 12:41pm UTC](https://discourse.processing.org/t/random-word-picker/10619/5 "2019-04-25T12:41:32Z")

</div>

-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

```auto
// 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.
}

```

---

<div class="post-metadata">

**Author:** ![mariamalz](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@mariamalz](https://discourse.processing.org/u/mariamalz)\
**Post date:** [April 26, 2019, 8:38am UTC](https://discourse.processing.org/t/random-word-picker/10619/6 "2019-04-26T08:38:01Z")

</div>

Added a download link!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 26, 2019, 9:36am UTC](https://discourse.processing.org/t/random-word-picker/10619/7 "2019-04-26T09:36:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mariamalz](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@mariamalz](https://discourse.processing.org/u/mariamalz)\
**Post date:** [June 26, 2019, 12:53pm UTC](https://discourse.processing.org/t/random-word-picker/10619/8 "2019-06-26T12:53:37Z")

</div>

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