If statements within an array of digits

I am trying to have different things happen depending on what digit is called in an array–in this case, different colored lines. I am not sure how to code the if statement to recognize a particular digit. This is what I have now:

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

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

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

When I try to run this code, I get the following error message:

“Cannot invoke equals(char) on the primitive type int”

When I get rid of the apostrophes around the numbers:

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

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

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

…I get the error:

“Cannot invoke equals(int) on the primitive type int”

Help!

1 Like

hello,

you don’t need to use the equals() method for the primitive types. just use == like

if (data[i] == 2) {
}
2 Likes

.equals() is important for Strings. Use == for ints and floats.

If you have many single value checks against the same variable this:

if (a == 1) {
}
else if (a == 2) {
}
else if (a == 3) {
}
//...

…then consider using a switch statement with case.

https://processing.org/reference/switch.html

Thank you. That did it!

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

You are missing one small but extremely important detail. Can you see it?

1 Like

Thank you, I got it. :grinning:

2 Likes