Need help; String[] and Methods

Hi, for my study i had a task which i have already completed (code below) i needed to draw four different names using a method. What i’ve made below works however how can i improve the method?

Code below doesn’t work its to illustrate what i want to achieve. Basically i want the new method to call an index number which stores the string and display that word so i can keep everything in one array.

String[] words = { "bla", "argh", "woohoo" };

void draw () {
 newMethod(wordNumber[1], 10, 10);
 println(newMethod);

}

// new method
void newMethod(String[] wordNumber, float x, float y) {
wordNumber = words;

}

To add to the stuff above: i’ve dabbled in Coding train concerning String array’s , split, join functions and using the for loop. However i’m still at banging at a wall for the thing i want to achieve.

This is my task.

PFont 
  myFont;
float 
  x, y, cX, cY, 
  hoeken, cHoeken, 
  linksX, linksY, rechtsX, rechtsY;

String 
  hallo, groet, n;

String[] 
  naam;

void setup() {
  size(600, 400);
  background(20);
  myFont = createFont("Georgia", 16);
  x = width;
  y = height;
  cX = x/2;
  cY = y/2;
  hoeken = sq(5);
  linksX = cX-cX+hoeken;
  linksY = cY-hoeken;
  rechtsX = cX+hoeken;
  rechtsY = cY+cY-hoeken;

  hallo = "Hallo ";
  groet = " hoe gaat het met je?";
  n = "Harry, Marianne, Ludo, Maaike";
  naam = split(n, ",");
}

void draw() {
  textFont (myFont);  
  naam0(linksX, linksY);
  naam1(rechtsX, linksY);
  naam2(linksX, rechtsY);
  naam3(rechtsX, rechtsY);
}

void naam0(float naamX, float naamY) {  
  text(hallo+naam[0]+groet, naamX, naamY);
}

void naam1(float naamX, float naamY) {  
  text(hallo+naam[1]+groet, naamX, naamY);
}

void naam2(float naamX, float naamY) {  
  text(hallo+naam[2]+groet, naamX, naamY);
}

void naam3(float naamX, float naamY) {  
  text(hallo+naam[3]+groet, naamX, naamY);
}

1 Like

:pineapple: Consider this example code:

String[] words = { "bla", "argh", "woohoo" };

void setup(){
  size(200,200);
  fill(255);
}

void draw () {
 background(0); 
 newMethod(1, 10, 10);
 newMethod(2, 40, 80);
}

void newMethod(int index, float x, float y) {
 text( words[index], x, y );
}

Notice that the index into the array is passed to the new function. There is no need to pass the array itself because it is in the global scope and can already be accessed inside the function.

3 Likes

Haha no way. I thought it would’ve been very complex. Thanks for the information i really appreciate it!