How to get a number array in processing_beginner

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.

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;
  }
}
1 Like
1 Like

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

A Java array’s length is fixed! We can’t add nor delete an array’s index once it’s created: :no_good_man:
Docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

If you need a dynamically length container, you’re better off w/ an ArrayList instead: :bulb:
Processing.org/reference/ArrayList.html
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html

2 Likes

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

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

3 Likes

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.