Can't find cause of "Unexpected token: void"

edited September 2014 in Questions about Code

I am doing a Blob tracking program right out of a book. I ran another earlier and the "void keyPressed()" coding worked. I put the same code in another program and get an "unexpected token:void" error although the book calls for same code. I'm guessing I missed something, or put in a typo further up the code. I must be looking through the error! Would appreciate a quick review. Thanks!

 //Blob Tracking   

import processing.video.*;

Capture webcam;
int threshold;
int topLeftX;
int topLeftY;
int bottomRightX;
int bottomRightY;

void setup()
{
  size( 640, 480);

  webcam = new Capture( this, width, heigh, 30 );
  webcam.start();

  threshold = 127;

  topLeftX = width;
  topLeftY = height;

  bottomRightX = 0;
  bottomRightY = 0;
}  

void draw()
{
  if ( webcam.available() )
  {
    webcam.read();
    image (webcam, 0, 0);

    loadPixels();

    int counter = 0;
    for (int j = 0; j <webcam.height; j++)
    {
      for (int i = 0; i < webcam.width; i++)
      {
        color c = webcam.pixels[counter];
        float b = brightness( c );
        if (b > threshold ) 
        {
          pixels[counter] = color( 255 );
          if (i > topLeftX )
          {
            topLeftX = i;
          }
          if ( j < topLeftY )
          {
            topLeftY = j;
            if ( i > bottomRightX )
            {
              bottomRightX = i;
            }
            if ( j > bottomRightY )
            {
              bottomRightY = j;
            }
          } 
           else
          {
            pixels[counter] = color( 0 );
          }
          counter++;
        }
      }

      updatePixels();

      noFill();
      stroke( 255, 0, 0 );
      strokeWeight( 2 );
      rect( topLeftX, topLeftY, bottomRightX - topLeftX, 
      bottomRightY - topLeftY );

      //reset tracking points
      topLeftX = width;
      topLeftY = height;
      bottomRightX = 0;
      bottomRightY = 0;

      fill( 255, 0, 0 );
      noStroke();
      rect( 10, 10, 110, 20 );
      fill( 255 );
      text( "Threshold: " + threshold, 14, 24 );
    }
  }

void keyPressed()
  {
    if (key ==CODED )
     {
      if (keyCode == UP )
        {
        threshold++;
        }
      if (keyCode == DOWN )
        {
        threshold--;
        }  
     }
  }
Tagged:

Answers

  • When posting code, plz highlight it and hit CTRL+K in order to format it for the forum! [-(

  • Answer ✓

    I think you will find that you need a } on line 92 to close the draw method.

  • edited September 2014

    Or maybe on line 70, to end one of your for loops in what looks like the right place.

  • 70 looks a good possibility too. Easy enough for you to try out both and see which works fou you.

  • Hint: in the PDE, when you click on a parenthesis, the matching one is highlighted. It can show when one is skipped. Also try and use the Auto format feature (Ctrl+T).

  • The } on line 92 worked. However, now I get a full, color picture and it should be black/white with a box around the face. Not there! Feel free to jump in, while I try to find error! Thanks for the reviews! I REALLY appreciate the help!

    The New Guy!!

  • I think the missing bracket was after line 66. Counter++ should be outside the condition.

  • Answer ✓

    We were all wrong it should be between 53 and 54 becaus this if statement is mot closed off

    51 if ( j < topLeftY )

    52 {

    53 topLeftY = j;

  • the indenting of that block and the two }}s on lines 61 and 62 makes that look intentional. but, you're right, could be a mistake.

Sign In or Register to comment.