# I need Help with save a int

**URL:** <https://discourse.processing.org/t/i-need-help-with-save-a-int/20934>\
**Category:** Processing for Android\
**Created:** [May 16, 2020, 9:25am UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934 "2020-05-16T09:25:34Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 16, 2020, 1:59pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/21 "2020-05-16T13:59:34Z")

</div>

\*\* May not apply to Android\*\* Let me know!

Read the reference:  
[https://processing.org/reference/saveStrings\_.html](https://processing.org/reference/saveStrings_.html)

And the expected error if you are not using an array[] of strings:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/ced6891fec596614456f236df417df7fbc13a363.png)

`:)`

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 16, 2020, 1:59pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/22 "2020-05-16T13:59:51Z")

</div>

On Android you save strings like [this](https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/saveStrings()).  
But I think for saving scores you better use SharedPreferences.  
After my lunch I’ll write some code, but now I’m hungry.

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 16, 2020, 2:07pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/23 "2020-05-16T14:07:40Z")

</div>

@noel can you Help me? 😂

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [May 16, 2020, 2:33pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/24 "2020-05-16T14:33:48Z")

</div>

@Rogo === the error you get is clearly explained: saveStrings() is waiting for 2 params, first one is a String (the name for your file), second one is not a String, it s an Array; so before saving you have to create some String[] as a global and add your score\_str to that. Now depending of your needs it could be more simple with android to use SharedPreferences API from android.  
Now, seeing your errors while coding, i think that firstly you have to code for java (not android!) then try to adapt it to Android.

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 16, 2020, 4:25pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/25 "2020-05-16T16:25:10Z")

</div>

@noel

I’ve tried it for a few hours now but it didn’t work. Please help

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 16, 2020, 4:42pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/26 "2020-05-16T16:42:09Z")

</div>

Try this code. (tested on Lollipop)

```auto
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.content.Context;
import android.app.Activity;

Activity act;
Context cnt;
SharedPreferences sp;
SharedPreferences.Editor editor;

int myScore;
String myScoreFile;

void setup () {
  background(0, 0, 150);
  textSize (40);
  textAlign (CENTER);
  act = this.getActivity();
  cnt = act.getApplicationContext();
  sp = PreferenceManager.getDefaultSharedPreferences(cnt);
  editor = sp.edit();
  myScoreFile = "TotalScores";
  myScore = loadScore(myScoreFile);
  text("Click here to gain points and save them.", width/2, height/4);
}

void settings() {
  fullScreen();
}

void draw () {
}

void mousePressed () {
  if (mouseY > height/2) {
    myScore++;
    saveScore(myScore, myScoreFile);
    background(0, 0, 150);
    text("Click here to gain points and save them.\n My saved score is "+str(myScore), width/2, height/4);
    text("Click here to show saved points.", width/2, 3*height/4);
  } else {
    myScore = loadScore (myScoreFile);
    background(0, 0, 150);
    text("Click here to gain points and save them.", width/2, height/4);
    text("Click here to show saved points.", width/2, 3*height/4);
  }
}

void saveScore(int score, String name) {
  //editor.clear();
  editor.putInt(name, score);
  editor.commit();
}

int loadScore(String name) {
  int getScore = sp.getInt(name, 0);
  return getScore;
}

void onPause() {
  super.onPause();
  saveScore(myScore, myScoreFile);
}

```

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 16, 2020, 5:53pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/27 "2020-05-16T17:53:29Z")

</div>

I dont understand this

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 16, 2020, 5:55pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/28 "2020-05-16T17:55:44Z")

</div>

> [@Rogo](#):
>
> this

what?..  
On APDE you have to run in “app mode” , otherwise the integer will not be saved when you exit the app…

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 16, 2020, 6:51pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/29 "2020-05-16T18:51:22Z")

</div>

I’ve tweaked the code above a bit . Try again.

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 16, 2020, 7:05pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/30 "2020-05-16T19:05:24Z")

</div>

Okay wait 🤩

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 16, 2020, 7:08pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/31 "2020-05-16T19:08:54Z")

</div>

okay wait now i understand the meaning more

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 16, 2020, 7:29pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/32 "2020-05-16T19:29:21Z")

</div>

I got it thank you 🙂

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 17, 2020, 8:17am UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/33 "2020-05-17T08:17:37Z")

</div>

@noel how can I remove what is on myScore from “myScore” if I click on it during construction? for example, a upgrade costs 1000 clicks

```auto
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.content.Context;
import android.app.Activity;

Activity act;
Context cnt;
SharedPreferences sp;
SharedPreferences.Editor editor;

int myScore;
String myScoreFile;
int bau;
float x = 100;
float y = 50;
float w = 150;
float h = 80;
String baufile;

void setup () {
  background(0, 0, 150);
  textSize (75);
  textAlign (CENTER);
  act = this.getActivity();
  cnt = act.getApplicationContext();
  sp = PreferenceManager.getDefaultSharedPreferences(cnt);
  editor = sp.edit();
  myScoreFile = "Scores";
  myScore = loadScore(myScoreFile);
  baufile = "build";
  bau = loadScore(baufile);
}

void settings() {
  fullScreen();
}

void draw(){
  myScore = loadScore(myScoreFile);
  bau = loadScore(baufile);
  background(0);
  text(str(myScore), width/2, height/14);
  fill(#FFFFFF);
  rect(x,y,w,h);
  text(str(bau), width/2, height/2);
}

void mousePressed () {
  if(mousePressed){
    if(mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h){
      ++bau;
      myScore--;
      redraw();
      saveScore(bau, baufile);
    }
  }
  myScore++;
  saveScore(myScore, myScoreFile);
}

void saveScore(int score, String name) {
  //editor.clear();
  editor.putInt(name, score);
  editor.commit();
}

int loadScore(String name) {
  int getScore = sp.getInt(name, 0);
  return getScore;
}

void onPause() {
  super.onPause();
  saveScore(myScore, myScoreFile);
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 17, 2020, 9:29am UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/34 "2020-05-17T09:29:31Z")

</div>

Simple.  
If bau is 6400 und fein = 1000  
bau = 6400-1000;  
saveScore(bau, baufile);

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 17, 2020, 10:25am UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/35 "2020-05-17T10:25:39Z")

</div>

@noel I dont underatand 😂

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 17, 2020, 5:22pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/36 "2020-05-17T17:22:53Z")

</div>

Within the app, there is a preference file where values can be stored.  
If your phone is rooted you can find it at this path:

> /data/data/com.calsignlabs.apde.sketchpreview\_preferences.xml

The file has a xml structure where you can see name of the key(label)  
In this case “TotalScores”, which can have a value like here .“25”  
The file looks like this:

```auto
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <int name="TotalScores" value="25" />
</map>

```

In the first code above you can find this label name “TotalScores”  
It is like a little box where you **change** the value when you use the function:

```auto
 void saveScore(int score, String name) {
  editor.putInt(name, score);
  editor.commit();
}

```

“score” is the integer value that will replace the existing value.  
To retrieve the value that is stored you use the function:

```auto
int loadScore(String name) {
  int getScore = sp.getInt(name, 0);
  return getScore;
}

```

That is all there is to it.  
When a player loses points, just subtract it from the existing value in the “box”

You can have more labels in this preference.xml file, like you already have done in your code,  
Just use another name for the key and the store value.  
As you see it’s actually very simple.

A better name for the function could be getScore() instead of loadScore() and change the name  
“myScoreFile” with myScoreLabel (or key).

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 17, 2020, 6:44pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/37 "2020-05-17T18:44:28Z")

</div>

I need like this

```auto
void mousePressed () {
  if(mousePressed){
    if(mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h){
      if(myScore > 9){
          myScore - 10;
          ++bau;
          saveScore(bau, baufile);
      }
    }else{
      myScore++;
      saveScore(myScore, myScoreFile);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 17, 2020, 7:04pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/38 "2020-05-17T19:04:31Z")

</div>

@Rogo I dont underatand 😂

You mean myScore -= 10;;

---

<div class="post-metadata">

**Author:** ![Rogo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rogo/32/12349_2.png) [@Rogo](https://discourse.processing.org/u/Rogo)\
**Post date:** [May 17, 2020, 7:34pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/39 "2020-05-17T19:34:47Z")

</div>

why are no 10 points deducted from “myScore”?

[![]( " - YouTube") ](https://www.youtube.com/watch?v=M3VZYkfSaEc)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 17, 2020, 8:55pm UTC](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934/40 "2020-05-17T20:55:10Z")

</div>

First thing to do is taking out

> myScore = loadScore(myScoreFile);  
> bau = loadScore(baufile);

of the draw() loop and place them in mousePressed()  
saveScore(bau, … in if  
saveScore(myScore, … in else

[Previous page](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934.md?page=1)

[Next page](https://discourse.processing.org/t/i-need-help-with-save-a-int/20934.md?page=3)
