# How to get a number array in processing\_beginner

**URL:** <https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759>\
**Category:** Beginners\
**Created:** [November 24, 2019, 9:30am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759 "2019-11-24T09:30:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [November 24, 2019, 9:30am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759/1 "2019-11-24T09:30:44Z")

</div>

Hi. I gave up on my electrostatic project and started a graph project. I need to receive an array to make a graph, but “ArrayIndexOutOfBoundsException: 1” keeps popping up.  
Can anyone tell me a way of getting a number array input in processing, like using for or something?  
Thank you.

P.S I’ll paste the code here for any kind person who will fix it or give me an alternative.

```auto
void setup(){
  background(0);
  size(1000, 1000);
  textSize(30);
  text("*GraphSaver Beta Instructions*\n\nPress the white button on the right top to end program.\n\n1.First enter the title.\n2.Then enter the number of rows.\n3.Then enter all of your data.\n4.A graph will be made.press enter to save and delete to erase it. \n\nWhen you finish reading click the screen.", 20, 30);
}

int num=0;
String title = "";
int data[]={0};
void draw(){
  //text("*Instructions*\n\nPress the white button on the right top to end program.\n\n1.First enter the number of rows.\n2.Then enter the title.\n3.Then enter all of your data.\n4.A graph will be made.press enter to save and delete to erase it. ", 10, 10);
  square(950, 0, 50);
  
  textSize(30);
  if(keynum==0)text(title, 100, 100);
  if(keynum==1)text(num-48, 100, 100);
  if(keynum==2 && imsi-1 == num)for(int i = 0; i <= imsi-1; i++)text(data[i], 100, 100);
}

void mousePressed(){//End process
  background(0);
  if(mouseX > 950 && mouseY < 50){
    fill(200, 11, 22);
    square(950, 0, 50);
    delay(5000);
    print("END");
  }
}

void mouseMoved(){
  if(mouseX > 950 && mouseY < 50){
    fill(200, 11, 22);
    square(950, 0, 50);
  }
  else {
    fill(255);
    square(950, 0, 50);
  }
}
int imsi=0;//temp
int keynum=0;//how nany times key has been pressed-0:title, 1: number of rows, 2 : data
void keyPressed(){//input
  background(0);
  if(key == ENTER){keynum++;return;}
  if(keynum == 0)title += key;
  if(keynum == 1){
    num = key;
  }
  if(keynum == 2){
    data[imsi++]=key;
  }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 24, 2019, 9:51am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759/2 "2019-11-24T09:51:40Z")

</div>

- [Forum.Processing.org/two/discussion/8080/why-use-arraylist-instead-of-array-with-append](http://Forum.Processing.org/two/discussion/8080/why-use-arraylist-instead-of-array-with-append)

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [November 24, 2019, 10:47am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759/3 "2019-11-24T10:47:14Z")

</div>

Umm… I don’t think that I understand it  
Sorry but can you like, explain it or something?  
Thank you.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 24, 2019, 10:55am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759/4 "2019-11-24T10:55:08Z")

</div>

A Java array’s _length_ is fixed! We can’t add nor delete an array’s index once it’s created: 🙅‍♂️  
[Docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html](http://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)

If you need a dynamically _length_ container, you’re better off w/ an ArrayList instead: 💡  
[Processing.org/reference/ArrayList.html](http://Processing.org/reference/ArrayList.html)  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html)

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [November 24, 2019, 10:57am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759/5 "2019-11-24T10:57:06Z")

</div>

oh! so I have to save the array’s content only as I make it?

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [November 24, 2019, 10:58am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759/6 "2019-11-24T10:58:14Z")

</div>

Maybe I should just save it in separate int s like int one, int two…?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 24, 2019, 11:47am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759/7 "2019-11-24T11:47:18Z")

</div>

- Java (and some libraries as well) offers several types of containers besides its vanilla array `[]`.  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/doc-files/coll-overview.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/doc-files/coll-overview.html)
- We should choose a container that best fits our needs.
- In your particular case, you need a container that holds `int` elements: `int data[]={0};`
- But at the same time, you need to increase its _length_ **size()** later: `data[imsi++]=key;`
- So the most appropriate container for that would be an IntList:  
[Processing.org/reference/IntList.html](http://Processing.org/reference/IntList.html)

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [November 25, 2019, 7:00am UTC](https://discourse.processing.org/t/how-to-get-a-number-array-in-processing-beginner/15759/8 "2019-11-25T07:00:32Z")

</div>

Aha! I understood.  
I learned C, so I wondered why an error popped out when making an array like “arr[5] = {0}”, But that was the problem!  
Thank you.
