Delete an element by entering a number and the following elements must move up

This array element (e-mails) shows up when you start the program - thats right. But I want to do something like: If I press e.g. the key ‘1’ I want that the program delete automatically the mailingListe element number 1 (i=1) and the following elements must move up and replace the new blank space. I tried it the keyPressed command as you can see at the last lines but it didnt function. Does someone knows the right commands for this code.

String [] mailingListe = new String[10];
int i,y;                                
PFont font;
String[] fontList = PFont.list();
String tmptext = "";

void setup()
  size(1000, 900);
  font = createFont("Times New Roman", 1);            
  textFont(font, 22);

void draw()
     for (i=0; i<10; i++)                         
         y = y+70;                       
         text(tmptext, 350,y);

void fillArray()
  mailingListe[0] = "";
  mailingListe[1] = "";
  mailingListe[2] = "";
  mailingListe[3] = "";
  mailingListe[4] = "";
  mailingListe[5] = "";
  mailingListe[6] = "";
  mailingListe[7] = "";
  mailingListe[8] = "";
  mailingListe[9] = "";

void keyPressed()        
  if ( key == '1-9')      
    mailingListe[i] = tmptext;  
    tmptext = " ";           
1 Like

First, I am making the assumption that this was homework and that you were tasked with implementing the function.
If you are simply looking for a tool that does the job, that would be a different answer. :grin:

Using keyPressed() is a nice way to do this, just remember that if your list is larger than 10 elements, you will have to change your solution.

Processing will not like comparing “key” to ‘1-9’ as that is not a ‘character’.
What you can do is something like this:

// characters ("char" data type) can be compared numerically also
if ((key >= '0') && (key <= '9'))
  // manipulate array here

Hopefully that helps. Let us know if you get stuck again.

1 Like

thank you for your helpful advice!