# Switching from keypressed to using an array

**URL:** https://discourse.processing.org/t/switching-from-keypressed-to-using-an-array/19053
**Category:** Coding Questions
**Created:** [March 26, 2020, 8:17pm UTC](https://discourse.processing.org/t/switching-from-keypressed-to-using-an-array/19053 "2020-03-26T20:17:18Z")
**Posts on this page:** 2
**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 26, 2020, 8:17pm UTC](https://discourse.processing.org/t/switching-from-keypressed-to-using-an-array/19053/1 "2020-03-26T20:17:18Z")

</div>

I have code that draws a multi-colored spiral pattern with different line lengths and colors depending on which number keys are pressed. I would like to change this to reading the numbers from an array, but I am having some trouble. I am new to processing so my code contains lines that I do not understand (added due to your helpful suggestions, Processing collective – many thanks). The keypressed code is as follows:

```auto
int lineSeg=10; //length of line segment
int xPos=0;
int yPos=0;
String exec=""; // A String exec gets recorded (in keyPressed()) and then evaluated (executed) in evaluateInputs().

void setup() {
size(800,800);
background(#FFFFFF);
strokeWeight (3);

}

//translate
//rotate(-HALF_PI);

void draw () {
translate(width/2, height/2);
rotate (-HALF_PI);
evaluateInputs();
}

void keyPressed() {

    if (key == '1' ||
        key == '2' ||
        key == '3' ||
        key == '4' ||
        key == '5' ||
        key == '6' ||
        key == '7' ||
        key == '8' ||
        key == '9' ||
        key == '0' ) {
        exec += key; //record keys
    }
}

void evaluateInputs() {
  //evaluate recorded string
    for (int i=0; i < exec.length(); i++) {
      
      float amtValue = map(i, 0, exec.length(), 0, 1);
      switch (exec.charAt(i)) {
        
        case '1':
        stroke(#F02947);//red
        rotate(-HALF_PI);
        tLine(lineSeg);
        break;
        
        case '2':
        stroke(#F029D6);//fucia
        rotate(-HALF_PI);
        tLine(2*lineSeg);
        break;
        
        case '3':
        stroke(#9229F0);//purple
        rotate(-HALF_PI);
        tLine(3*lineSeg);
        break;
        
        case '4':
        stroke(#293EF0);//blue
        rotate(-HALF_PI);
        tLine(4*lineSeg);
        break;
        
        case '5':
        stroke(#29E1F0);//turquoise
        rotate(-HALF_PI);
        tLine(5*lineSeg);
        break;
        
        case '6':
        stroke(#29F077);//lime green
        rotate(-HALF_PI);
        tLine(6*lineSeg);
        break;
        
        case '7':
        stroke(#549B05);//moss
        rotate(-HALF_PI);
        tLine(7*lineSeg);
        break;
        
        case '8':
        stroke(#EFF029);//yellow
        rotate(-HALF_PI);
        tLine(8*lineSeg);
        break;
        
        case '9':
        stroke(#F09029);//pumpkin
        rotate(-HALF_PI);
        tLine(9*lineSeg);
        break;
        
        case '0':
        stroke(0);//black
        fill(0);
        ellipse(0, 0, lineSeg, lineSeg);
        break;
      } //switch
    } //for
} //funct

void tLine(int x) {
  tLineDraw(x);
}

void tLineDraw(int x){
  line(0, 0, x, 0);
  translate(x, 0);
  }

```

My attempt to switch this code over to reading from an array is here:

```auto
int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int lineSeg=10; //length of line segment
int xPos=0;
int yPos=0;

void setup() {
size(800,800);
background(#FFFFFF);
strokeWeight (3);

}

//translate
//rotate(-HALF_PI);

void draw () {
translate(width/2, height/2);
rotate (-HALF_PI);
}

for (int i=0; i < data.length; i++) {
      
 // float amtValue = map(i, 0, exec.length(), 0, 1); // I have no idea what this does; someone told me to do it
      switch (data[i]) { //instead of if/then format
        
        case '1':
        stroke(#F02947);//red
        rotate(-HALF_PI);
        tLine(lineSeg);
        break;
        
        case '2':
        stroke(#F029D6);//fucia
        rotate(-HALF_PI);
        tLine(2*lineSeg);
        break;
        
        case '3':
        stroke(#9229F0);//purple
        rotate(-HALF_PI);
        tLine(3*lineSeg);
        break;
        
        case '4':
        stroke(#293EF0);//blue
        rotate(-HALF_PI);
        tLine(4*lineSeg);
        break;
        
        case '5':
        stroke(#29E1F0);//turquoise
        rotate(-HALF_PI);
        tLine(5*lineSeg);
        break;
        
        case '6':
        stroke(#29F077);//lime green
        rotate(-HALF_PI);
        tLine(6*lineSeg);
        break;
        
        case '7':
        stroke(#549B05);//moss
        rotate(-HALF_PI);
        tLine(7*lineSeg);
        break;
        
        case '8':
        stroke(#EFF029);//yellow
        rotate(-HALF_PI);
        tLine(8*lineSeg);
        break;
        
        case '9':
        stroke(#F09029);//pumpkin
        rotate(-HALF_PI);
        tLine(9*lineSeg);
        break;
        
        case '0':
        stroke(0);//black
        fill(0);
        ellipse(0, 0, lineSeg, lineSeg);
        break;
      } //switch
    } //for

void tLine(int x) {
  tLineDraw(x);
}

void tLineDraw(int x){
  line(0, 0, x, 0);
  translate(x, 0);
  }

```

Right now my error is “expecting EOF, found ‘for’,” but I suspect there is more wrong with the code than just that.

Any help would be appreciated!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 26, 2020, 8:39pm UTC](https://discourse.processing.org/t/switching-from-keypressed-to-using-an-array/19053/2 "2020-03-26T20:39:54Z")

</div>

You got a wrong closing bracket } before the for loop

It’s closing `draw()` but that’s wrong (place the closing bracket } after the for-loop)

**Second point**

Also, in switch just say `case 1:` without the `' '`

(`' '` is with chars but you have ints in your array)

**Full code**

```auto

int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int lineSeg=10; //length of line segment
int xPos=0;
int yPos=0;

void setup() {
  size(800, 800);
  background(#FFFFFF);
  strokeWeight (3);
}

//translate
//rotate(-HALF_PI);

void draw () {
  translate(width/2, height/2);
  rotate (-HALF_PI);

  for (int i=0; i < data.length; i++) {

    // float amtValue = map(i, 0, exec.length(), 0, 1); // I have no idea what this does; someone told me to do it
    switch (data[i]) { //instead of if/then format

    case 1:
      stroke(#F02947);//red
      rotate(-HALF_PI);
      tLine(lineSeg);
      break;

    case 2:
      stroke(#F029D6);//fucia
      rotate(-HALF_PI);
      tLine(2*lineSeg);
      break;

    case 3:
      stroke(#9229F0);//purple
      rotate(-HALF_PI);
      tLine(3*lineSeg);
      break;

    case 4:
      stroke(#293EF0);//blue
      rotate(-HALF_PI);
      tLine(4*lineSeg);
      break;

    case 5:
      stroke(#29E1F0);//turquoise
      rotate(-HALF_PI);
      tLine(5*lineSeg);
      break;

    case 6:
      stroke(#29F077);//lime green
      rotate(-HALF_PI);
      tLine(6*lineSeg);
      break;

    case 7:
      stroke(#549B05);//moss
      rotate(-HALF_PI);
      tLine(7*lineSeg);
      break;

    case 8:
      stroke(#EFF029);//yellow
      rotate(-HALF_PI);
      tLine(8*lineSeg);
      break;

    case 9:
      stroke(#F09029);//pumpkin
      rotate(-HALF_PI);
      tLine(9*lineSeg);
      break;

    case 0:
      stroke(0);//black
      fill(0);
      ellipse(0, 0, lineSeg, lineSeg);
      break;
    } //switch
  } //for
}

void tLine(int x) {
  tLineDraw(x);
}

void tLineDraw(int x) {
  line(0, 0, x, 0);
  translate(x, 0);
}

```
