Int wont be saved in my txt file

Hello. @TimeLex helped my with my data issue and it worked. Now I added two values. Int and String. The String is being saved but the Int doesnt want to be saved anymore. It always resets to 0 when I restart the program

boolean maySaveData;
int count = 0;
String count1 = "Hello";
String countFilePath;

void setup() {
  fullScreen();
  
  countFilePath = dataPath("count.txt");
  
  checkPermission();
  loadAndRequestCount();
}
void loadAndRequestCount() {
  if(maySaveData) {
    checkFileExists(countFilePath, "saveCount");
    loadCount();
    loadCount1();
  }else{
    requestPermission("android.permission.WRITE_EXTERNAL_STORAGE");
    checkPermission();
  }
}
void checkPermission() {
  maySaveData = hasPermission("android.permission.WRITE_EXTERNAL_STORAGE");
}
void checkFileExists(String path, String callback) {
  File file = new File(path);
  if(!file.exists())
    method(callback);
  else if(loadStrings(path).length == 0)
    saveCount();
}
void saveCount() {
  saveStrings(countFilePath, new String[]{str(count)});
  saveStrings(countFilePath, new String[]{count1});
}
int loadCount() {
  count = int(loadStrings(countFilePath)[0]);
  return count;
}
String loadCount1() {
  count1 = loadStrings(countFilePath)[0];
  return count1;
}
void draw() {
  background(255);
  textAlign(CENTER, CENTER);
  if(maySaveData) {
    textSize(100);
    fill(#000000);
    text(count, width/2, height/2);
    text(count1, width/2, height/1.5);
  }else{
    textSize(30);
    fill(#FF0000);
    text("Please give permission to save data.\nOtherwise the program can't run correctly.\nPress to request again.", width/2, height/2);
    
    checkPermission();
  }
}

void mousePressed() {
  if(maySaveData) {
    count++;
    count1 = "hi";
    saveCount();
  }else
    loadAndRequestCount();
}

You are using the same path twice.

Are you not writing over the first file when saving #2 ?

What should I do? I don’t know how to to this using saveStrings only once there. Is that the reason why it doesnt work?

In the IDE you can use ctrl-t to open your folder. Check which files are there and what they contain, using notepad/editor.

saveStrings/loadStrings is made to work with arrays of String.

My Idea

In theory, one line holds one data (so one String and one int in your case), next line holds the next data set (pair). (In your case, you use only one line, which is ok)

To achieve a pair in one line, the content of one line is separated by comma:

Replace this:

with

void saveCount() {
  String[] a1 = new String[1]; 
  a1[0] =  count1 + "," + trim(str(count)); 
  saveStrings(countFilePath, a1);
}

Loading

instead of having loadCount and loadCount1 you can have load():

void load() {
  // load array 
  String[] a1 = loadStrings(countFilePath); 

  // split the line #0 up:
  String[] a2 = split(a1[0], ","); 

  // assign its two elements to count1, and count
  count1 = a2[0];
  count   =  int(a2[1]); // EDITED 
}// func

Now the Int went next bewtween to the String but it does save the value but the Int text over the String text keeps resetting to 0. Looks like there are two Int text’s now. Sorry for my bad explanation, maybe I should send a Screenshot

Delete your text file. Use my Loading function above and delete your two loading functions.

Try again.

It gives an Error at

count = a2[1];

Error:
Cannot convert from String to int

My bad.

This is better:

count = int(a2[1]);

For some reason both wont save anymore

Did you look into the file with notepad/editor?

Should look like this:

Hello,0

Please debug your code and post entire code

I should have told you before but I am using Android device

I know,but can you look into the file directly via the system?

never mind, just debug your code

post the entire code

I gotta go

boolean maySaveData;
int count = 0;
String count1 = "Hello";
String countFilePath;

void setup() {
  fullScreen();
  
  countFilePath = dataPath("count.txt");
  
  checkPermission();
  loadAndRequestCount();
}
void loadAndRequestCount() {
  if(maySaveData) {
    checkFileExists(countFilePath, "saveCount");
    //loadCount();
    //loadCount1();
  }else{
    requestPermission("android.permission.WRITE_EXTERNAL_STORAGE");
    checkPermission();
  }
}
void checkPermission() {
  maySaveData = hasPermission("android.permission.WRITE_EXTERNAL_STORAGE");
}
void checkFileExists(String path, String callback) {
  File file = new File(path);
  if(!file.exists())
    method(callback);
  else if(loadStrings(path).length == 0)
    saveCount();
}
void saveCount() {
  String[] a1 = new String[1]; 
  a1[0] =  count1 + "," + trim(str(count)); 
  saveStrings(countFilePath, a1);
}
void load() {
  // load array 
  String[] a1 = loadStrings(countFilePath); 

  // split the line #0 up:
  String[] a2 = split(a1[0], ","); 

  // assign its two elements to count1, and count
  count1 = a2[0];
  count =  int(a2[1]);
}
void draw() {
  background(255);
  textAlign(CENTER, CENTER);
  if(maySaveData) {
    textSize(100);
    fill(#000000);
    text(count, width/2, height/2);
    text(count1, width/2, height/1.5);
  }else{
    textSize(30);
    fill(#FF0000);
    text("Please give permission to save data.\nOtherwise the program can't run correctly.\nPress to request again.", width/2, height/2);
    
    checkPermission();
  }
}

void mousePressed() {
  if(maySaveData) {
    count++;
    count1 = "hi";
    saveCount();
  }else
    loadAndRequestCount();
}

ok.

You save on mouse click.

But you never load.

Can you make 3 mouse buttons on the screen for New Save and Load to test it?

I think there is no need for it anymore since it is working now. I forgot to add load();

Thank you for helping me!:grin:

1 Like

I just found out that it doesnt work on my game for some reason. I get the error:
OutOfBoundsException length=1 index=1

At the moment we have only one line in the array

instead of [1] use some more lines

(Please tell us which line the error occurred)

I dont know the reason but it somehow got solved. Now I have a huge problem. When I open sketch properties in APDE, the whole program crashes

1 Like

I can’t help you with that