Array stops working. Also how do I not let random phrases/words stop from repeating

Hi all,

I am trying to make the game catch phrase/charades on processing. All I am trying to do is generate some random words/phrases when I start my sketch. I already have this following code that does generate the words/phrases i have in my array. However; I get this error: "ArrayIndexOutOfBoundException: Array index out of range: 6". I have no clue how to fix it and also further proceed with adding a timer when i Start my sketch. This is the code I have so far:-

StringList phrase;
//String phrase[] = { 
//  "love", "treasure", "garbage", 
//  "NYUSH", "photograph", "this game sucks", 
//  "i hate finals", "wtf", "IMA", 
//  "B1", "Shanghai", "strictly cookies", 
//  "PoH", "Asia", "Indirectly Be Evil", 
//  "i give up", "Jinqiao", 
//  "Puxi", "Pudong", 
//  "identity", "Macbook", "lifehub", 
//  "Modu", "Scoreboard", "Mint"

  //There are a total of 25 "How to" in this String
  //Note: One string is only capable of holding 25
//};
PFont f; 
//int phr = int(random(phrase.length));
//This generates a random position in String phrase[]
int counter;
int timer;
int x;
int s1,s2;
int ready;
int pressed;
int array;
int prevArray;


void setup() {
  size(500, 200);
  f = createFont("Times New Roman", 20, true);
  fill(#000000);
  phrase= new StringList();
  phrase.append("love");
  phrase.append("hate");
  phrase.append("fuckthis");
  phrase.append("identity");
  phrase.append("strictly cookies");
  phrase.append("Scoreboard");
  phrase.append("Jinqiao");
  phrase.append("Puxi");
  background(255);
  x=0;

}

void draw(){
  if (keyPressed==true && ready>=500){
    println(prevArray);
    if(x<=2){
    x++;
  }
  background(255);
  if(x>2){
  //phrase.remove[array];
 // phrase.remove(array);
  textFont(f, 16);
  prevArray=array;
  array=(int)random(phrase.size());
  text(phrase.get(array), 10, 100);
  phrase.remove(prevArray);

  }
  pressed=millis();
  }

  if(x==0){
    text("Welcome, push the button to start",width/2,height/2);
  }
  if (x==1){
    text("instructions",width/2,height/2);
  }
  if (x==2){
    text("ready?",width/2,height/2);
  }

  ready=millis()-pressed;




  //counter=millis()-timer;
  // if(counter>=5000){
  // timer=millis();
  //}

 //println(counter);
  //int timer(int timerLength) {
  //int remainingTime = timerLength-millis();

  //if(remainingTime/1000>0){
    //int actualTime = (remainingTime/1000);
    //return actualTime;
  // }
  //else {
    //time = false;
    //return 0;     
  }
//}

//void keyPressed() {
//  if(x<=2){
//    x++;
//  }
//  background(255);
//  if(x>2){
//  textFont(f, 16);                                     
//  text(phrase[(int)random(phrase.length)], 10, 100);
//  }
//}

If anyone can help me with this I would be forever in debt! Will be highly appreciated. Looking forward for help and support.

Tagged:

Answers

  • Answer ✓

    Edit post, highlight code, press ctrl-o to format your comment.

    Which line is the error on? It means that you are trying to read element 6 of an array that doesn't have enough elements in it. Which probably means you aren't initialising something you should be initialising.

    But first, fix the formatting so we can read the code.

  • I just edited as you said. The error is for line 61: phrase.remove(prevArray);

    Hope you can assist me now :)

  • Add some debug that prints out prevArray and the current size of the array

  • edited December 2016

    The error comes from the flow of the program-

    • Initially, prevArray and array are 0, and the no of elements in phrase is 8 (corresponding to max index of 7).
    • After first frame, array is some random number b/w 0 and 7(inclusive). prevArray is still 0. Now, phrase has 7 elements.

    Can you see a problem already? You may end up accessing phrase.get(7) in next frame when phrase has only 7 elements.

    • If no error occurs, then after second frame, prevArray is the same as array was at the end of previous frame. phrase now has only 6 elements.

    Further problem? Yes. Now you may end up trying to remove an element with index larger than the size of phrase can handle.

Sign In or Register to comment.