# Random rows/columns in a csv file and print text with typewriter effect

**URL:** https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889
**Category:** Coding Questions
**Created:** [July 22, 2019, 3:16pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889 "2019-07-22T15:16:50Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 22, 2019, 3:16pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/1 "2019-07-22T15:16:50Z")

</div>

Hi all,

I am starting on a project that involves using data from a large csv. For now I am just playing with the processing Table/row documentation and have created a mammals.csv file containing

```auto
amount, species, name 
0, Capra circus, Goat
1, Panthera Pardus, Leopard
2, Equus Zebra, zebra

```

What I am trying to achieve is

1. Randomly select one row
2. First print the amount of that row
3. Clear screen
4. Then print name of that same row
5. Clear screen
6. Start process again

With the the below code both amount and name are appearing at the same time

```auto
Table table;

void setup() {
  
  table = loadTable("mammals.csv", "header");
  size(500,500);
  background(0);

}
void draw() {

background(0);

  TableRow row = table.getRow((int)random(0,3)); //print a random row 
   String name = row.getString("name"); // prints name for row 
   textSize(32); //text size 
   fill(255,0,0);
   text(name, 100,100); // what to print and where 
   delay(1000); //delay for 5 seconds 
   
   
   //background(0); //background used to clear 
   String amount = row.getString("amount"); //a string with amount 
   textSize(32); //text size 
   text(amount, 100,100); 
   delay(1000); 

}

```

I have tried to insert a background() between the String name and String amount but that is just returning amount.

Any suggestions much appreciated!

---

<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: [July 22, 2019, 3:25pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/2 "2019-07-22T15:25:06Z")

</div>

draw doesn’t update the screen throughout

draw just updates the screen once at the end of draw

Hence, your approach won’t work.

**Instead**

instead, have a boolean situationShowAmount before `setup()`

in draw():

if(situationShowAmount) show amount, else show the name

instead of using delay try a timer

before setup():  
`int timer=0;`

In draw():

```auto
if(millis()-timer > 1200) {
    // toggle
    situationShowAmount = ! situationShowAmount ; // toggle 
    timer=millis(); 
}

```

Chrisir

---

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 22, 2019, 3:44pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/3 "2019-07-22T15:44:24Z")

</div>

Hi Chrisir,

Thanks for the reply.

I think I understand what you mean. if over 1200 millis seconds have passed and amount does not equal amount, show amount??

However, I am confused as to why I should do this before setup() as I am unsure how to call it in draw in relation to amount and name.

Any further clarification would be great!

---

<div class="post-metadata">

### Author: ![Gus\_Fermin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gus_fermin/32/5851_2.png) [@Gus\_Fermin](https://discourse.processing.org/u/Gus_Fermin)
#### Post date: [July 22, 2019, 3:46pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/4 "2019-07-22T15:46:01Z")

</div>

Hola Chrisir, I learned this also through your input on my code, thank you very much for your help 🙂 the way you use `booleans` right before an `if` statement, is that what is called a ‘Flag’? and/or a ‘switch’?

---

<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: [July 22, 2019, 3:52pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/5 "2019-07-22T15:52:50Z")

</div>

> [@Gus\_Fermin](#):
>
> you use `booleans` right before an `if` statement

I’d say I use a boolean variable in or with an if-clause.

It’s called a flag. (a marker if something is true or not)

(`switch()` is a command in processing similar to `if...else if...else if... else....`. Some might call a flag a switch (myself) but I think flag is better: [Bit field - Wikipedia](https://en.wikipedia.org/wiki/Bit_field))

(if your boolean `situationShowAmount ` needs more states than two, just go for an `int` which can have a lot of states (0,1,2,3…). Used often for the states of a program (e.g. for Intro Screen, Game, Game Over Screen, High Score Screen, Help Screen…))

**Remark**

What I was trying to say was:

- make a timer to toggle `situationShowAmount ` (if-clause 1) and
- evaluate `situationShowAmount ` in another if-clause (if-clause 2), acting accordingly (showing amount or name)

Chrisir

```auto
Table table;
TableRow row;
boolean situationShowAmount=true; 
int timer;

String amount="", name=""; 

void setup() {
  size(500, 500);
  background(0);
  table = loadTable("mammals.csv", "header");
  readData(); 
  timer=millis();
}

void draw() {

  background(0);

  if (situationShowAmount) {
    //show amount
    textSize(32); //text size 
    text(amount, 100, 100);
  } else {
    textSize(32); //text size 
    fill(255, 0, 0);
    text(name, 100, 100); // what to print and where
  }

  // ---------------------------------------------------

  if (millis()-timer > 3200) {
    // toggle
    situationShowAmount = 
      ! situationShowAmount;  
    readData(); 
    timer=millis();
  }//if
}

void readData() {
  if (situationShowAmount) {
    //read amount 
    row = table.getRow((int)random(0, 3)); //print a random row 
    amount = row.getString("amount"); //a string with amount
  } else {
    //read name 
    // row = table.getRow((int)random(0, 3)); //print a random row 
    name = row.getString("name"); // prints name for row
  }
}
//

```

---

<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: [July 22, 2019, 3:56pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/6 "2019-07-22T15:56:12Z")

</div>

@[OptOut](https://discourse.processing.org/u/OptOut)

I corrected my mistake and made a full sketch

---

<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: [July 22, 2019, 4:08pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/7 "2019-07-22T16:08:17Z")

</div>

I corrected my code again

besides from the headline

```auto
amount, species, name 

```

I had to remove the space signs (and the last invisible one behind the name)

---

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 22, 2019, 4:16pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/8 "2019-07-22T16:16:04Z")

</div>

Doh! Silly me…that makes sense, thanks for the help Chrisir!

---

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 22, 2019, 4:37pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/9 "2019-07-22T16:37:27Z")

</div>

Sorry to bother you again @Chrisir, I just realised an error as a result of lack of clarity on my OP.

The amount that appears should be the corresponding amount to the name that appears so

Goat 1  
Leopard 2  
Zebra 3

From what I can see, the amount is selecting a random number that is not related to the name.

---

<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: [July 22, 2019, 4:49pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/10 "2019-07-22T16:49:28Z")

</div>

that was an error I corrected on my code above.

Please try again.

---

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 22, 2019, 4:52pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/11 "2019-07-22T16:52:35Z")

</div>

Apologies…that’s returning a nullpointererror on line 49:  
`name = row.getString("name"); // prints name for row`

---

<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: [July 22, 2019, 4:53pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/12 "2019-07-22T16:53:07Z")

</div>

in read data we repeated this after the else:

```auto
   row = table.getRow((int)random(0, 3)); //print a random row    

```

Bad.

---

<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: [July 22, 2019, 4:55pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/13 "2019-07-22T16:55:05Z")

</div>

my Bad.

before setup():

`boolean situationShowAmount=true;`

must be **true** so we start with _reading the amount_ before using it

---

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 22, 2019, 4:59pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/14 "2019-07-22T16:59:18Z")

</div>

Thanks for your patience…think that’s it 😀

---

<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: [July 22, 2019, 4:59pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/15 "2019-07-22T16:59:51Z")

</div>

you can also use

`getRowCount()`

see here

`row = table.getRow((int)random(0, table.getRowCount())); //get a random row`

---

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 25, 2019, 2:46pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/16 "2019-07-25T14:46:40Z")

</div>

Hi everybody,

I decided to post this question here rather than start a new thread as it is related to previous posts…hope that is ok, and if not I’ll be happy to create a new thread.

So I have now changed the output of the content. Instead of having name followed by amount I would like them to display at the same time, however, I would like to display this content using a typewriter effect, so one character at a time.

One name + corresponding amount to appear to on screen at the same time, but appearing character by character and then randomly selecting a new row…

The csv file has been changed slightly (multiple digits)

```auto
amount, species, name 
0.45.753.2311, Capra circus, Goat
1.11232.34564.56, Panthera Pardus, Leopard
2.435.654.684, Equus Zebra, zebra

```

The following code has been cobbled together from various sources, it’s almost there (but at the same time, probably not!)

```auto
Table table; //creates table 
TableRow row; // creates row 
String amount="", name="", nameAmount=""; //string 
int counter = 0 ; //set counter to zero 

void setup() {
  size(500, 500);
  background(0);
  //frameRate(1); //frameRate for speed of text?
  table = loadTable("mammals.csv", "header"); //table to load 

}

void draw() {
  
  background(0); //set background colour 
  fill(255, 0, 0); //set text colour 
  textSize(32); //text size 
  readData(); // calls readData Fucntion 
}

void readData() {

    row = table.getRow((int)random(0, 3)); //selects a random row from 
    amount = row.getString("amount"); //a string with amount
    name = row.getString("name"); // prints name for row
    String nameAmount = amount + " " + name; // Lets me put name + amount together 
  
   println (nameAmount); //prints to console, just for me
  
  if (counter < nameAmount.length()) // if counter is less than the amount of char of NameAmount
    counter++; //increase counter by 1 
  text(nameAmount.substring(0, counter), 100, 100, width, height); 
  delay (1000);

  }

```

The problems with the above code:

- The numbers and letter that appear seemed to be jumbled from other rows - although the end number and name does appear. correctly but only for this first iteration

- I also get a string index out of range error which I think is occurring in the subString()

Any suggestions would be great!

---

<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: [July 25, 2019, 4:30pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/17 "2019-07-25T16:30:18Z")

</div>

Don’t use delay

Never

(Except with external devices)

use my timer instead

Don’t allow a new word to be chosen as long as the old one isn’t finished

---

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 25, 2019, 4:36pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/18 "2019-07-25T16:36:08Z")

</div>

Thanks @Chrisir will give it a try 🙂

---

<div class="post-metadata">

### Author: ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)
#### Post date: [July 25, 2019, 6:09pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/19 "2019-07-25T18:09:56Z")

</div>

sorry to bother you again @Chrisir could I ask you to elaborate on  
‘Don’t allow a new word to be chosen as long as the old one isn’t finished’

I’m taking that to mean that I should create another boolean so I can make a conditional  
Ahead is my pseudocode

```auto
if(newString != finishedString){
then keep iterating through newString
}

```

but not sure if my logic is correct.

---

<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: [July 25, 2019, 6:17pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/20 "2019-07-25T18:17:55Z")

</div>

Yes!

I meant you have one variable that is named showHowManyLettersOfThatData (in your wording `counter`)

You increase this

When it’s \>= the length of the String, set it to 0 and choose a new word

**pseudo-code:**

```auto
if(timer............) {
      showHowManyLettersOfThatData++; 
}

if( showHowManyLettersOfThatData >= nameAmount.length() ) {
     showHowManyLettersOfThatData =0; 

    row = table.getRow((int)random(0, 3)); //selects a random row from 
    amount = row.getString("amount"); //a string with amount
    name = row.getString("name"); // prints name for row
    String nameAmount = amount + " " + name; // Lets me put name + amount together 
  
   println (nameAmount); //prints to console, just for me

}

```

[Next page](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889.md?page=2)
