If statements within an array of digits

I tried to convert my code to using “switch” but it did not work. I like the idea because I am working up to a project where each digit from 0 to 9 in an array of 1,000 digits will perform a different action. Using if/then statements, I created the following code, which works:

int[] data = {1, 2, 3, 4, 5};
background(#1CB9FF);
size(800,800);
strokeWeight (5);

ellipse (10*data[0], 10*data[1], 20, 20);

for (int i=0; i<data.length; i++) {
  
  if (data[i] == 2) {
    stroke(255);//white line
    line(10*data[i], 0, 10*data[i], 800); }
    
  else if (data[i] == 3) {
    stroke(#FC0824);//red line
    line(10*data[i], 0, 10*data[i], 800); }
    
  else {
    stroke(0);//black line
    line(10*data[i], 0, 10*data[i], 800); }
}

Below is what I came up with trying to use “switch” instead:

int[] data = {1, 2, 3, 4, 5};
background(#1CB9FF);
size(800,800);
strokeWeight (5);

ellipse (10*data[0], 10*data[1], 20, 20);


for (int i=0; i<data.length; i++) {
  
  switch(data) {
  
    case 2:
    stroke(255);//white line
    line(data[i], 0, data[i], 100); 
    break;
    
    case 3:
    stroke(#FC0824);//red line
    line(data[i], 0, data[i], 100); 
    break;
    
    default:
    stroke(0);//black line
    line(data[i], 0, data[i], 100);
    break;
  }
}

When I try to run the above code, I get an error message that says:

“Cannot switch on a value of type int[].”

Any idea what is happening or how to fix it?

1 Like