Why do I get array index out of bounce?...ㅡㅡ

edited May 2014 in Using Processing
Bubble [] bubbles;

void setup() {
  size(1280,720);
  smooth();

  String[] data = loadStrings("data.txt");
  bubbles = new Bubble[data.length];

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

    float[] values = float(split(data[i],","));
    bubbles[i] = new Bubble (values [0], values[1], values[2]); //change this value to display 46 values
  }
  println("values[0]");
}

void draw () {
  background(255);
  //display and move all bubbles

  for(int i = 0; i < bubbles.length; i++) {
    bubbles[i].display();
    bubbles[i].drift();
  }
  bubbles[3].drift();
}

//A class to describe a "Bubble"
class Bubble {
  float x,y;
  float diameter;

  float speed;
  float r,g;

  //the constructor intializes the color and size
  //location is filled randomly

  Bubble(float r_,float g_, float diameter_) {
    x = random(width);
    y = height;
    r = r_;
    g = g_;
    diameter = diameter_;
  }

  //display the Bubble

  void display() {
    noStroke();
    fill(r,g,255,150);
    ellipse(x,y,diameter,diameter);
  }

  //move the bubble

  void drift() {
    y += random(-3,-0.1);
    x += random(-1,1);
    if(y < diameter*2) {
      y = height + diameter*2;
    }
  }
}

My data file is this


90281, 19149, 90790, 19861, 91348, 20576, 91890, 21339, 92352, 22169, 92644, 23128, 92731, 24233, 92650, 25435, 92398, 26716, 91977, 28066, 91397, 29489, 90681, 30982, 89846, 32511, 88762, 34015, 87624, 35438, 86535, 36781, 85365, 38076, 84200, 39308, 13373, 83115, 40496, 12895, 82047, 41716, 80791, 42994, 79475, 44272, 78267, 45521, 77047, 46710, 75868, 47803, 74778, 48761, 73785, 49560, 72870, 50230, 72002, 50802, 71122, 51329, 70146, 51884, 69057, 52503, 67922, 53118, 66845, 53643, 65901, 53987, 65103, 54143, 64420, 54146, 63785, 54047, 63153, 53900, 62516, 53722, 61838, 53544, 61081, 53416, 60222, 53354, 59278, 53336, 58281, 53333, 57273, 53303,

56267, 53251

please help me...

It too hard ; (

Answers

  • edited May 2014 Answer ✓

    I am not sure if this is what you wanted but there are no errors, I modified your setup():

    void setup() {
      size(1280, 720);
      smooth();
    
      // Try printing data's length, there is only one element
      String[] data = loadStrings("data.txt");
      //println("Length of data: "+data.length);
    
      // Split data[0] by ","
      float[] values = float(split(data[0], ","));
    
      // Assign bubbles as an array of the same length as values
      // This is the number of values in data.txt
      bubbles = new Bubble[values.length];
    
      for (int i = 0; i < bubbles.length; i++) {
        bubbles[i] = new Bubble(values[0], values[1], values[2]);
        //println("Bubble "+i+": "+values[0]+", "+values[1]+", "+values[2]);
      }
    }
    

    Edit: Note, this is what I used for data.txt:

    90281, 19149, 90790, 19861, 91348, 20576, 91890, 21339, 92352, 22169, 92644, 23128, 92731, 24233, 92650, 25435, 92398, 26716, 91977, 28066, 91397, 29489, 90681, 30982, 89846, 32511, 88762, 34015, 87624, 35438, 86535, 36781, 85365, 38076, 84200, 39308, 13373, 83115, 40496, 12895, 82047, 41716, 80791, 42994, 79475, 44272, 78267, 45521, 77047, 46710, 75868, 47803, 74778, 48761, 73785, 49560, 72870, 50230, 72002, 50802, 71122, 51329, 70146, 51884, 69057, 52503, 67922, 53118, 66845, 53643, 65901, 53987, 65103, 54143, 64420, 54146, 63785, 54047, 63153, 53900, 62516, 53722, 61838, 53544, 61081, 53416, 60222, 53354, 59278, 53336, 58281, 53333, 57273, 53303, 56267, 53251

  • edited May 2014 Answer ✓

    I wonder when it's red and green value and diameter

    shouldn't the data.txt look like this:

    25,155,10
    255,210,22
    

    etc. ?

    As asimes said, this

      String[] data = loadStrings("data.txt");
    

    works only when there are multiple lines in the text file (multiple paragraphs)

    the data in one line are split by "," then

    (or did the forum software corrupt the data you posted?)

  • Really thank you guys : ) it worked.

  • @ asimes It has no errors, but I think this code just visualizes the first line and that's it.

  • Answer ✓

    Yes, this doesn't change per Bubble (each gets 0, 1, and 2):

    bubbles[i] = new Bubble(values[0], values[1], values[2]);

    I didn't modify that line of code because I didn't understand what you wanted it to do.

  • Line 14 into the for-loop

  • data[i]

  • edited May 2014 Answer ✓

    here; this has no txt file but has a string array instead.

    But when the content of the txt file is as expected, it should work as well.

    I expect the content of the txt files to be:

    25,155,10
    255,210,22
    

    ;-)

    Bubble [] bubbles;
    
    void setup() {
      size(1280, 720);
      smooth();
    
      // String[] data = loadStrings("data.txt");
    
      String[] data = { 
        "25,155,10", 
        "255,210,22", 
        "25,155,30", 
        "155,210,42"
      };
    
      println ("Array length is now: "+data.length+".");
    
      // Assign bubbles as an new array of the same length as data.
      // This is the number of LINES in data.txt.
      bubbles = new Bubble[data.length];
    
      for (int i = 0; i<bubbles.length; i++) {
        // we loop over all lines of data now 
        // Split data[i] by ","
        float[] values = float(split(data[i], ","));
        // Assign a new bubble in the array with the numbers from that line 
        bubbles[i] = new Bubble (values [0], values[1], values[2]); //change this value to display 46 values
        println("Line #"+i+" is "+data[i]+" and contains as 1st value: " + values[0] + ".");
      }
      println ("End of setup()    -------");
    }
    
    void draw () {
      background(255);
      //display and move all bubbles
      for (int i = 0; i < bubbles.length; i++) {
        bubbles[i].display();
        bubbles[i].drift();
      }
      bubbles[3].drift();
    }
    
    //A class to describe a "Bubble"
    class Bubble {
      float x, y;
      float diameter;
    
      float speed;
      float r, g;
    
      //the constructor intializes the color and size
      //location is filled randomly
    
      Bubble(float r_, float g_, float diameter_) {
        x = random(width);
        y = height;
        r = r_;
        g = g_;
        diameter = diameter_;
      }
    
      //display the Bubble
    
      void display() {
        noStroke();
        fill(r, g, 255, 150);
        ellipse(x, y, diameter, diameter);
      }
    
      //move the bubble
    
      void drift() {
        y += random(-3, -0.1);
        x += random(-1, 1);
        if (y < diameter*2) {
          // restart the bubble 
          y = height + diameter*2;
        }
      }
    } // class
    // ================================
    
Sign In or Register to comment.