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.
Thanks!

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


void setup()
{
  size(1000, 900);
  textSize(15);
  background(180);       
  fill(45);
  printArray(fontList);
  font = createFont("Times New Roman", 1);            
  textFont(font, 22);
  fillArray();
}

void draw()
{
     i=0;                               
     y=100;                              
     for (i=0; i<10; i++)                         
                                                
      {
         text(mailingListe[i],350,y);           
         y = y+70;                       
         
         text(tmptext, 350,y);
      }
}

void fillArray()
{
  mailingListe[0] = "test1@gmail.com";
  mailingListe[1] = "test2@gmail.com";
  mailingListe[2] = "test3@gmail.com";
  mailingListe[3] = "test4@gmail.com";
  mailingListe[4] = "test5@gmail.com";
  mailingListe[5] = "test6@gmail.com";
  mailingListe[6] = "test7@gmail.com";
  mailingListe[7] = "test8@gmail.com";
  mailingListe[8] = "test9@gmail.com";
  mailingListe[9] = "test10@gmail.com";
}


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!