How to use text() command to print string array to screen

Hello

Long story short: I’m porting my python program to processing to add a GUI. Here is my code so far:

int i = 0;

String currentName = "";

int z = i+1;

String names[] = new String[100];


void setup(){
  size(700,500);
}

void draw(){
  background(0);
  textAlign(CENTER);
  textSize(22);
  text("Welcome to the TechMaster Industries TechHelp Desk \n Please type in your name followed by ENTER to \n reserve a spot in the queue",350,50);
  textSize(32);
  text(currentName,350,160);
  textAlign(LEFT);
  text("Current Queue",10,250);
}

void keyPressed(){
   if (key == BACKSPACE){
    currentName= currentName.substring(0, max(0, currentName.length()-1));    }
   else if (key == ENTER){
     names[i] = currentName;
     currentName = "";
     println(names[i]);
     i= i+1;
   }
   else{
   currentName += key;
}
}

I want to use the text() command to print out the names[] array. I’ve tried putting in text(names[i],350,250); with no luck

Anyone know of a way to do this? Thanks in advance

1 Like
for ( int i = 0 ; i < 15 ; i++ ) text( names[i],20, 20+i*30 );
1 Like

Issue: I’m getting NullPointerException when I put that in my code

New Code:

int i = 0;

String currentName = "";

int z = i+1;

String names[] = new String[100];


void setup(){
  size(700,500);
}

void draw(){
  background(0);
  textAlign(CENTER);
  textSize(22);
  text("Welcome to the TechMaster Industries TechHelp Desk \n Please type in your name followed by ENTER to \n reserve a spot in the queue",350,50);
  textSize(32);
  text(currentName,350,160);
  textAlign(LEFT);
  text("Current Queue",10,250);
  for(int i = 0 ; i < 15 ; i++ ) text(names[i],20, 20+i*30);
  

}

void keyPressed(){
   if (key == BACKSPACE){
    currentName= currentName.substring(0, max(0, currentName.length()-1));    }
   else if (key == ENTER){
     names[i] = currentName;
     currentName = "";
     println(names[i]);
     i= i+1;
   }
   else{
   currentName += key;
}
}

Hello,

You need to initialize the array otherwise it is a “null” and you get that error.

Default array values:

  // Uncomment this to initialize array with a string:
  //String letter = "put String here";
  //for(int i = 0 ; i < 15 ; i++ ) names[i] = letter;
  
  for(int i = 0 ; i < 15 ; i++ ) 
    {
    if (names[i] != null)               //Checks for a null character
      text(names[i], 20, 20+i*30);
    else                                     
      text("null", 20, 20+i*30);
    }

:slight_smile:

2 Likes

Thank you so much, This was exactly what I was looking for

1 Like