Error message in this Array problem i don't know how to resolve

In Dan Shiffman’s book Learning Processing, p. 179, example 9-10

I have typed in the code for this example, and have carefully checked to make sure no keystroking errors. However I keep getting errors in the void rollover() function area. I think there’s an extra curly bracket but it matches the code in the book.


Stripe[] stripes = new Stripe[10];

void setup() {
  size(200, 200);
  for (int i = 0; i<stripes.length; i++) {
    stripes[i] = new Stripe();
  }
}
void draw() {
  background(100);
  for (int i =0; i < stripes.length; i++) {
    stripes[i].rollover(mouseX, mouseY);
    stripes[i].move();
    stripes[i].display();
  }
}
class Stripe {
  float x;
  float y;
  float w;
  boolean mouse;

  Stripe() {
    x = 0;
    speed = random(1);
    w = random(10, 30);
    mouse = false;
  }

  void display() {
    if (mouse) {
      fill(255);
    } else {
      fill(255, 100);
    }
    noStroke();
    rect(x, 0, w, height);
  }
void move() {
  x += speed;
  if (x > width + 20) x =-20;
}

void rollover(int mx, int my) { 
  {
    //left edge is x, right edge is x + w
    if (mx > x && mx < x + w)
      mouse = true;
  } else {
    mouse = false;
  }
}
}
1 Like

to find those: click ctrl-t in processing to get auto-indents

often we can spot the error then

error was:

void rollover(int mx, int my) {
{

speed was not defined


Stripe[] stripes = new Stripe[10];

void setup() {
  size(200, 200);
  for (int i = 0; i<stripes.length; i++) {
    stripes[i] = new Stripe();
  }
}
void draw() {
  background(100);
  for (int i =0; i < stripes.length; i++) {
    stripes[i].rollover(mouseX, mouseY);
    stripes[i].move();
    stripes[i].display();
  }
}

// ====================================================

class Stripe {
  float x;
  float y;
  float w;
  float speed; 
  boolean mouse;

  // constr 
  Stripe() {
    x = 0;
    speed = random(1);
    w = random(10, 30);
    mouse = false;
  }

  void display() {
    if (mouse) {
      fill(255);
    } else {
      fill(255, 100);
    }
    noStroke();
    rect(x, 0, w, height);
  }
  void move() {
    x += speed;
    if (x > width + 20) x =-20;
  }

  void rollover(int mx, int my) {
    //left edge is x, right edge is x + w
    if (mx > x && mx < x + w) {
      mouse = true;
    } else {
      mouse = false;
    }
  }
}
1 Like

I have now defined the speed, but am still receiving the same error message:

Stripe[] stripes = new Stripe[10];

void setup() {
size(200, 200);
for (int i = 0; i<stripes.length; i++) {
stripes[i] = new Stripe();
}
}
void draw() {
background(100);
for (int i =0; i < stripes.length; i++) {
stripes[i].rollover(mouseX, mouseY);
stripes[i].move();
stripes[i].display();
}
}

class Stripe {
float x;
float speed;
float w;
boolean mouse;

Stripe() {
x = 0;
speed = random(1);
w = random(10, 30);
mouse = false;
}

void display() {
if (mouse) {
fill(255);
} else {
fill(255, 100);
}
noStroke();
rect(x, 0, w, height);
}

void move() {
x += speed;
if (x > width + 20) x =-20;
}

void rollover(int mx, int my) {
{
//left edge is x, right edge is x + w
if (mx > x && mx < x + w)
mouse = true;
} else {
mouse = false;
}
}
}

One { too many

Okay…?

I tried deleting that second bracket, but still an error message: “expecting {, found ‘else’”

I think there may be a typo in the Shiffman book?

UPDATE TO POST
Confirming there is a typo in this code in the Shiffman book.
Placement of brackets sh’d be:

void rollover(int mx, int my) {
    if (mx > x && mx < x + w) {
      mouse = true;
    } else {
      mouse = false;
    }
}
3 Likes

Congrats! You solved it!

1 Like