# \[SOLVED\] Uneven Data Entry by sketch

**URL:** <https://discourse.processing.org/t/solved-uneven-data-entry-by-sketch/8449>\
**Category:** Coding Questions\
**Created:** [February 16, 2019, 2:12pm UTC](https://discourse.processing.org/t/solved-uneven-data-entry-by-sketch/8449 "2019-02-16T14:12:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harvious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/harvious/32/3610_2.png) [@harvious](https://discourse.processing.org/u/harvious)\
**Post date:** [February 16, 2019, 2:12pm UTC](https://discourse.processing.org/t/solved-uneven-data-entry-by-sketch/8449/1 "2019-02-16T14:12:29Z")

</div>

Hello all,

In my sketch, I have a bunch of balls bouncing around. I am saving the X, Y values of each ball, and the XSpeed, and YSpeed value of each ball every time draw() runs.

I use the following code :

```auto
PrintWriter hist1;
PrintWriter hist2;

hist1 = createWriter("spatial_density.txt");
hist2 = createWriter("Velocity_distribution.txt");

```

```auto
void draw(){
  
  background(255);

  for (int i = 0; i < elec.length; i++){
    elec[i].update(elec);
    elec[i].move();
    elec[i].bounce();
    hist1.println(nf(elec[i].getX(),0,2) +","+ nf(elec[i].getY(),0,2) );
    hist2.println(nf(elec[i].getDX(),0,2) +","+ nf(elec[i].getDY(),0,2) );
   
  }
  hist1.println("------------ F " + j);
  hist2.println("-------------F " + j);
  j+=1;

```

At first I thought I would end the program after j reaches a threshold value, however even that produces uneven data in the text files: i.e., for example, spatial\_density file will have, say, data up to j = 800, and the velocity\_distribution, only 600.

I also used the time module to exit the program after a certain time has elapsed, but that had the same results.

My canvas is 700\*600 big and the balls are 2 pixels wide.

I would want the data files to be of even order ie : x,y and xpeed, yspeed value of all balls upto j = same value.

What seems to be the problem?

---

<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:** [February 16, 2019, 3:14pm UTC](https://discourse.processing.org/t/solved-uneven-data-entry-by-sketch/8449/2 "2019-02-16T15:14:29Z")

</div>

start with the reference  
[https://processing.org/reference/createWriter\_.html](https://processing.org/reference/createWriter_.html)  
and basically test first with 2 files and the same content ( and test it real slow )

```auto
PrintWriter output1,output2;

int j,posx,posy;

void setup() { // Create a new file in the sketch directory
  output1 = createWriter("data/pos1.txt");
  output2 = createWriter("data/pos2.txt");
  frameRate(1);
}

void draw() {
  output1.println("x "+posx+" y " +posy); // Write the coordinate to the file
  output2.println("x "+posx+" y " +posy); // Write the coordinate to the file
  println("x "+posx+" y " +posy); // Write the coordinate to the console
  posx++;
  posy++;
  j++;
  if ( j >100 ) finish();
}

void finish() {
  output1.flush(); // Writes the remaining data to the file
  output1.close(); // Finishes the file
  output2.flush(); // Writes the remaining data to the file
  output2.close(); // Finishes the file
  exit(); // Stops the program
}

```

that works nice here: ( processing 3.5.3 // OS: Raspbian )  
so my question is how you do the stop?

---

<div class="post-metadata">

**Author:** ![harvious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/harvious/32/3610_2.png) [@harvious](https://discourse.processing.org/u/harvious)\
**Post date:** [February 16, 2019, 3:37pm UTC](https://discourse.processing.org/t/solved-uneven-data-entry-by-sketch/8449/3 "2019-02-16T15:37:32Z")

</div>

Hello @kll,

Thank you for your reply. It seems that I was not “writing the remaining data” to the file before the program closed, and the file.

The “finish” program is just what I needed.
