# If statements within an array of digits

**URL:** <https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507>\
**Category:** Beginners\
**Created:** [March 8, 2020, 6:18pm UTC](https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507 "2020-03-08T18:18:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichelO](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@MichelO](https://discourse.processing.org/u/MichelO)\
**Post date:** [March 8, 2020, 6:18pm UTC](https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507/1 "2020-03-08T18:18:11Z")

</div>

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:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [March 8, 2020, 6:21pm UTC](https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507/2 "2020-03-08T18:21:06Z")

</div>

hello,

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

```auto
if (data[i] == 2) {
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 9, 2020, 12:19am UTC](https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507/3 "2020-03-09T00:19:15Z")

</div>

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

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

```auto
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](https://processing.org/reference/switch.html)

---

<div class="post-metadata">

**Author:** ![MichelO](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@MichelO](https://discourse.processing.org/u/MichelO)\
**Post date:** [March 24, 2020, 8:28pm UTC](https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507/4 "2020-03-24T20:28:08Z")

</div>

Thank you. That did it!

---

<div class="post-metadata">

**Author:** ![MichelO](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@MichelO](https://discourse.processing.org/u/MichelO)\
**Post date:** [March 24, 2020, 8:58pm UTC](https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507/5 "2020-03-24T20:58:54Z")

</div>

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:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 25, 2020, 12:00am UTC](https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507/6 "2020-03-25T00:00:02Z")

</div>

> [@MichelO](#):
>
> if (data[i] == 2)

> [@MichelO](#):
>
> switch(data) { case 2:

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

---

<div class="post-metadata">

**Author:** ![MichelO](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@MichelO](https://discourse.processing.org/u/MichelO)\
**Post date:** [March 26, 2020, 7:54pm UTC](https://discourse.processing.org/t/if-statements-within-an-array-of-digits/18507/7 "2020-03-26T19:54:40Z")

</div>

Thank you, I got it. 😀
