arrays - Processing - How to log Strings in txt file? -


im getting more , more frustrated on why not doing want to. need have text file logs last 10 buttons user pressed, tried in 2 ways but…

…this code saves last button pressed:

string [][] buttonspressed = { {"pressed one"}, {"pressed two"} }; string[] sum;  void setup() {   size(700, 350);   sum = loadstrings("scores.txt"); }  void draw() { }  void keypressed() {   if ((key=='1')) {      savestrings("scores.txt", buttonspressed[0]);   }    if ((key=='2')) {      savestrings("scores.txt", buttonspressed[1]);   }    if ((key == enter)) {     printarray(sum);   } } 

…this code saves buttons pressed overwrites when run sketch again (probably has createwriter):

printwriter sum;  void setup() {   size(700, 350);   sum = createwriter("scores.txt"); }  void draw() { }  void keypressed() {   if ((key=='1')) {      sum.println("pressed one");   }    if ((key=='2')) {      sum.println("pressed two");   }    if ((key == enter)) {     sum.flush();      sum.close();     string[] lines = loadstrings("scores.txt");     (int = 0; <= 9; i++) {         printarray("[" + + "] " + lines[i]);     }   } } 

is there simple way without using libraries ? please me in right direction :)

break problem down smaller steps.

step 1: @ beginning of program, read in existing history file. store them in kind of data structure, array or arraylist.

step 2: make data structure keep track of last 10 buttons pressed. you'll need write code adds newest button pressed end of data structure, , if data structure contains more 10 items, have remove oldest item. maybe in separate example sketch print data structure console , don't worry outputting file yet.

step 3: output data structure file. don't have worry appending file because data structure contains entire history, can overwrite file entire data structure. you'll want every time data structure changes. again, maybe in separate example program before worrying data coming from: maybe make program outputs array of random numbers every time user clicks?

focus on 1 small step @ time, , try creating small example programs each step. when stuck, can ask specific question , provide mcve, , we'll go there. luck.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -