0

My code returns an error when I run it, telling me that I have "one too many { characters without a } to match it", but I've checked and rechecked and triple-rechecked and even had someone else check for me, to no avail.

class Ball {
  //Global Vars
  //float x=0;
  //float y=0;
  //float speedx = random(-5,5);
  //float speedy = random(-1,1);

  Vec3D loc = new Vec3D (0, 0, 0);
  Vec3D speed = new Vec3D (random(-4, 4), random(-1, 1), 0);

  Vec3D acc = new Vec3D();

  Vec3D grav = new Vec3D (0, random(0.05, 0.25), 0);

  //construct
  Ball(Vec3D _loc) {

    loc = _loc;
  }

  //functions
  void run() {
    display();
    move();
    bounce();
    //  gravity();
    lineBetween();
    flock();
  }

  void display() {
    stroke(0);
    ellipse(loc.x, loc.y, 20, 20);
  }


  void move() {
    // x += speedx;
    // y += speedy;

    speed.addSelf(acc);
    speed.limit(6);
    loc.addSelf(speed);
    acc.clear();
  }

  void bounce() {
    if (loc.x > width) {
      speed.x = speed.x*-1;
    }
    if (loc.x < width-width) {
      speed.x = speed.x*-1;
    }
    if (loc.y > height) {
      speed.y = speed.y*-1;
    }
    if (loc.y < height-height) {
      speed.y = speed.y*-1;
    }
  }

  void gravity() {
    //speedy += 0.15;

    speed.addSelf(grav);
  }

  void lineBetween() {
    //ballCollection
    for (int i=0; i<ballCollection.size();i++) {
      Ball other = (Ball) ballCollection.get(i);
      float distance = loc.distanceTo(other.loc);
      if (distance > 0 && distance < 80) {
        stroke(255, 0, 255);
        strokeWeight(0.2);
        line(loc.x, loc.y, other.loc.x, other.loc.y);
      }
    }
  }

  void flock() {
    separate();
    // cohesion();
    // align();
  }

  void separate(float magnitude) {
    Vec3D steer = new Vec3D();
    int count = 0;

    for (int i=0; i<ballCollection.size();i++) {
      Ball other = (Ball) ballCollection.get(i);
      float distance = loc.distanceTo(other.loc);
      if (distance > 0 && distance < 40) {

        Vec3D diff = loc.sub(other.loc);
        diff.normalizeTo(1.0/distance);

        steer.addSelf(diff);
         count++;
      }
    }
  }

  if (count>0) {
    steer.scaleSelf(1.0/count);
  }

  steer.scaleSelf(magnitude);
  acc.addSelf(steer);
}

The error message highlights line 106;

if (count>0) {

I've recreated the error on another machine, but seen the code used in a tutorial video without any issue. Any and all help would be greatly appreciated :)

BenjiHare
  • 15
  • 7
  • 1
    are you using an IDE like eclipse??.. if yes, then use ctrl+a and ctrl+shift+f to format the code.. else you could also use Notepad++.. avoid writing code in normal txt files... unformatted code will be hard to read.. – TheLostMind Feb 20 '14 at 09:19
  • I'm writing in the Processing editor, regularly auto-formatting anything I miss with CTRL+T :) – BenjiHare Feb 20 '14 at 10:39
  • I know this is solved but you can use the PDE X (I think that's what its called) to give you on the fly code completion and syntax checking. Its still in beta but its going to become the next Processing IDE aka PDE :P Here's some info: http://www.mkmoharana.com/2013/09/announcing-pde-x.html – Nico Feb 20 '14 at 14:28

4 Answers4

3

I think your problem is in the line 103, there is one extra }. The if(count>0) line is outside of the method.

user2424380
  • 1,393
  • 3
  • 16
  • 29
2

count variable is local variable but, you have used it outside of function. steer and acc also same. Update it like below;

void separate(float magnitude) {
    Vec3D steer = new Vec3D();
    int count = 0;

    for (int i=0; i<ballCollection.size();i++) {
      Ball other = (Ball) ballCollection.get(i);
      float distance = loc.distanceTo(other.loc);
      if (distance > 0 && distance < 40) {

        Vec3D diff = loc.sub(other.loc);
        diff.normalizeTo(1.0/distance);

        steer.addSelf(diff);
         count++;
      }
    }

    if (count>0) {
        steer.scaleSelf(1.0/count);
    }

      steer.scaleSelf(magnitude);
      acc.addSelf(steer);
  }
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
1

You have done a good job indenting, so the location of your braces are correct (you may have used Edit, autoformat). I suggest to my programming students that they comment 'end' curly braces to prevent such issues:

void separate(float magnitude) {
  Vec3D steer = new Vec3D();
  int count = 0;

  for (int i=0; i<ballCollection.size();i++) {
    Ball other = (Ball) ballCollection.get(i);
    float distance = loc.distanceTo(other.loc);
    if (distance > 0 && distance < 40) {

      Vec3D diff = loc.sub(other.loc);
      diff.normalizeTo(1.0/distance);

      steer.addSelf(diff);
      count++;
    }  // end if distance
  }  // end for
}  // end separate method
0

If you use an IDEA, you can easy to find the compile error, the if(count>0) is out of a method, it seems that you add a "}" before this statement by mistake.

jason
  • 364
  • 1
  • 3
  • 10