# Can I use the for loop to write 9 rows at the time?

**URL:** https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553
**Category:** Processing
**Created:** [March 15, 2021, 10:14pm UTC](https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553 "2021-03-15T22:14:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [March 15, 2021, 10:14pm UTC](https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553/1 "2021-03-15T22:14:46Z")

</div>

I am writing to CSV and it goes well, but there is a lot of data I need to generated.  
So I tried the for loop. But it writes and overwrites on the same first row instead of giving me 9 rows populated with individual data.

```auto
void SaveToFile ()

{ table.clearRows();
  TableRow newRow = table.addRow();
   newRow.setInt("MACH ID", table.getRowCount() - 1);
   for (int i=1; i<=9; i++)
 {
  newRow.setInt("MACH ID", i);
  newRow.setInt("CUMM UPTIME", CummUp[i]);
  newRow.setInt("CYCLES TODAY", Cycles[i]);
  newRow.setString("TIME", TimeStamp);
  //newRow.setString("DATE", DateStamp);
 }
  ////////////////////////

println("ROW COUNT= " + table.getRowCount()); 

  Filename = "/data/"+"Infinity_Report_"+DateStamp + ".csv"; //DateStamp
  saveTable(table, Filename);
  //println("Settings Saved");
  
} 

What to do?
Thanks 
Mitch

```

---

<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: [March 15, 2021, 10:27pm UTC](https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553/2 "2021-03-15T22:27:25Z")

</div>

> [@laptophead](#):
>
> ```auto
> TableRow newRow = table.addRow();
>  
> 
> ```

move this after the for…{

**AND**

```auto
   newRow.setInt("MACH ID", table.getRowCount() - 1);

```

delete this.

but KEEP:

```auto
 newRow.setInt("MACH ID", i);

```

**Remark**

arrays usually start with 0, not 1, check your for-loop

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [March 15, 2021, 11:57pm UTC](https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553/3 "2021-03-15T23:57:32Z")

</div>

Yey, that worked…

Thanks , In my case I have to start with 1.

Danke Schoon

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [March 16, 2021, 12:34am UTC](https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553/4 "2021-03-16T00:34:35Z")

</div>

Since you’re so good with arrays, etc…  
I am trying to get a time stamp when a condition from the serial happens and it works for a min, then I get a nullPointer.  
Any ideas?

```auto
 if ( list.length >=2 )
        {
          Mach_ID= int (list [0]);
          LastUptime= int(trim(list [1]));

          if (Mach_ID == 1)
            {CummUp [1] = (CummUp [1]+ LastUptime)/1000;
            Cycles [1] = (Cycles[1]+1);
            TimeStampArrFun(1);

            }

//// then the function
void TimeStampArrFun(int Mach_ID)
{

  sec = second(); // Values from 0 - 59
  min = minute(); // Values from 0 - 59
  hrs = hour(); // Values from 0 - 23
  day = day(); // Values from 1 - 31
  mo = month(); // Values from 1 - 12
  yr = year(); 

  String S = str(sec); // Values from 0 - 59
  String M = str(min);
  String H = str(hrs); // Values from 0 - 59
  String D = str(day);// Values from 0 - 59
  String MO = str(mo); // Values from 0 - 59
  String Y = str(yr);// Values from 0 - 59

  TimeStampArr[Mach_ID] = H+":"+ nf(min,2)+":"+ nf(sec,2) +" ";
  println (Mach_ID +"Stamp "+TimeStampArr[Mach_ID]);
}

```

What am I doing wrong?

The array is defined in the setup as :  
String []TimeStampArr;

Also I tried to write the function as a String Arrray etc… trying to return the time stamp but it did not work out…

Thanks again  
Mitch

---

<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: [March 16, 2021, 8:18am UTC](https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553/5 "2021-03-16T08:18:06Z")

</div>

I assume that you write over the end of the List

TimeStampArr

Like when you gave it an length of 500 when you try t  
write element 501 it crashes

---

<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: [March 16, 2021, 8:28am UTC](https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553/6 "2021-03-16T08:28:10Z")

</div>

you don’t really need an array here

A simple string would be sufficient

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [March 16, 2021, 5:35pm UTC](https://discourse.processing.org/t/can-i-use-the-for-loop-to-write-9-rows-at-the-time/28553/7 "2021-03-16T17:35:39Z")

</div>

Actually I need the array, there are 10 different time stamps.  
The probelm was in declaring the array in the definition.  
I tried the easy  
String []TimeStampArr;  
but it did not work, so I had to go the long way:  
String TimeStampArr[]= {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};// 10 vals

Now it works. Lots more code…

Thanks
