Why does my program return the error "ArrayIndex Out Of Bounds Exception: 4"?

Because it takes into account the current length of the array genPart

Try it

Example

String blocked = "iA";
String inlava = "iB";
String nearsick = "iC";

String moverandom = "oA";
String movewest = "oB";
String moveeast = "oC";
String movenorth = "oD";
String movesouth = "oE";

String[] input = {blocked, inlava, nearsick};

String[] genome = new String[51];

float[] x = new float[51];
float[] y = new float[51];

boolean beingblocked = false;
String[] genpart; // = new String[204];


void setup() {

  noLoop();

  // fullScreen();
  size(333, 333);

  for (int i= 0; i<=50; i++) {
    genome[i] = "i"+char(int(random(65, 67))) +"1" + "o"+char(int(random(65, 69))) +"1" + "i"+char(int(random(65, 67))) + "1" + "o"+char(int(random(65, 69)));
  }
  for (int i=0; i<51; i++) {
    genpart = split(genome[i], '1');
    // print(genpart[i] + " ");
    // using i2 here within the for loop with i
    for (int i2=0; i2<genpart.length; i2++) {
      print(genpart[i2] + " ");
    }
    println();
  }
}

void draw() {
  if (genpart[0]==blocked || 
    genpart[2]==blocked && 
    genpart[1]== moverandom || 
    genpart[3]==moverandom) {
    if (beingblocked) {
      x[0]= x[0]+(int(random(-1, 1)))*10;
    }
  }
}
//

1 Like