# Save variable in a loop

**URL:** <https://discourse.processing.org/t/save-variable-in-a-loop/4654>\
**Category:** Beginners\
**Created:** [October 19, 2018, 5:28pm UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654 "2018-10-19T17:28:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [October 19, 2018, 5:28pm UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654/1 "2018-10-19T17:28:40Z")

</div>

Hello,  
I am a beginner in programming.  
I created a simple program that shows my problem :

```auto
int A=0;

void setup () {
  size(400,400);
}

void draw () {
  background(0);
  if(mousePressed) {
    A=A+1;
  }
  text(A,200,200);
}

```

I would like that when I close the program, the variable A does not start from 0 but from before the closing of the program.

Thank you.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 19, 2018, 5:43pm UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654/2 "2018-10-19T17:43:11Z")

</div>

1. [Processing.org/reference/loadStrings\_.html](http://Processing.org/reference/loadStrings_.html)
2. [Processing.org/reference/saveStrings\_.html](http://Processing.org/reference/saveStrings_.html)

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [October 20, 2018, 6:32pm UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654/3 "2018-10-20T18:32:50Z")

</div>

Hello,

Thank you for your answer but I don’t understand how it works and the Google translation does not help me. Is there a simpler example ?

Thank you.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 20, 2018, 6:56pm UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654/4 "2018-10-20T18:56:13Z")

</div>

```java
/**
 * Load & Save Variable (v1.0.1)
 * GoToLoop (2018/Oct/20)
 * https://Discourse.Processing.org/t/save-variable-in-a-loop/4654/4
 */

int a;

void setup() {
  size(300, 200);
  noLoop();

  fill(-1);
  textAlign(CENTER, CENTER);
  textSize(0100);

  final String[] aFile = loadStrings("a.txt");
  if (aFile != null) a = int(aFile[0]);
}

void draw() {
  background((color) random(#000000));
  text(a, width>>1, height>>1);
}

void mousePressed() {
  ++a;
  redraw = true;
}

@Override void exit() {
  saveStrings(dataPath("a.txt"), new String[] { str(a) });
  super.exit();
}

```

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [October 20, 2018, 7:37pm UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654/5 "2018-10-20T19:37:45Z")

</div>

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [October 21, 2018, 9:19am UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654/6 "2018-10-21T09:19:33Z")

</div>

Hello,

Thank you for your example.  
I still have a question : can we encrypt the data so that the user can not change them ?

Thank you.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [October 21, 2018, 9:33am UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654/7 "2018-10-21T09:33:06Z")

</div>

If you save it as a .txt file, even though you could encrypt it, the User Would still be able to change it, just not make much sense out of it. I assume that you wanted to encrypt it just to make it unchangable, But if not, there should be some encryption methods available. Easiest Would probably be to search online for Java Encryption methods, or to just add +1 in each Char of the String of your file, which Would also work, But be a very Weak encryption. But if it’s only to avoid fast changes to the file, then that could also Suffice.

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [October 22, 2018, 6:59am UTC](https://discourse.processing.org/t/save-variable-in-a-loop/4654/8 "2018-10-22T06:59:41Z")

</div>

Thank you so much 😉
