We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Help with error: unexpected token: void
Page Index Toggle Pages: 1
Help with error: unexpected token: void (Read 4322 times)
Help with error: unexpected token: void
Aug 18th, 2008, 5:23am
 
Can someone help me?

I developed a program that worked fine drawing one shape. Now I am trying to convert it to one using arrays to draw more shapes. I *think* I wrote the code correctly, but now it won't run. All I get is the "void setup(){" line highlighted and an "unexpected token: void" error message.

I don't know what this means, or how to fix the problem.

George
Re: Help with error: unexpected token: void
Reply #1 - Aug 18th, 2008, 6:31am
 
Paste your code into the forum. You may have some code above your setup() declaration that does not terminate cleanly.
Re: Help with error: unexpected token: void
Reply #2 - Aug 18th, 2008, 7:12am
 
I discovered the solution, but I don't understand quite why it works. Here's a shortened tryout:

int numShapes = 2;
boolean[] changingColors;

void setup(){
 changingColors = new boolean [numShapes];
 size (200, 200);
 background(0);
 smooth();
 for(int i = 0; i < numShapes; i++){
   changingColors[i] = true;
 }
}

What produced the error is having the line <changingColors = new boolean [numShapes];> above the setup() function. When this creation line is above setup(){, I get the error.
In the section on arrays, p 303 in Reas and Fry, declaring, creating and assigning arrays is illustrated BEFORE setup() (example 33-04.) So I don't understand why I am getting the error.

George
Re: Help with error: unexpected token: void
Reply #3 - Aug 20th, 2008, 2:22am
 
You did not declare the type of the variable changingColors in your alternative line. In Java (the backing language of Processing), you must define the types of your variables before or simultaneous with assigning them a value:

changingColors = new boolean [numShapes]; // Error
boolean[] changingColors = new boolean [numShapes]; // OK

See p. 38, 3-01.
Re: Help with error: unexpected token: void
Reply #4 - Mar 13th, 2009, 8:34pm
 
//
//
//
for (int i = 0; i < 10; i ++) {}

void setup() {
//
//
//

I get "unexpected token: void", when I run this.
I have isolated the problem at these two lines. Only when this for() is enabled does the problem occur. What is happening here?
Re: Help with error: unexpected token: void
Reply #5 - Mar 13th, 2009, 8:44pm
 
You can't put a for() loop--or any other code besides variable, class, and method declarations--outside of a method.  What's happening is that you are confusing the Java parser by feeding it stuff it isn't expecting.  Your for loop itself is syntacically correct, but you have put it in an unexpected, and disallowed, place.

The problems you're having are ones of basic Java syntax.  There are plenty of good Java tutorials out on the web; I would suggest that you go look them up, as having a good grasp of Java's syntax will be an enormous help to you in using Processing.
Re: Help with error: unexpected token: void
Reply #6 - Mar 13th, 2009, 8:47pm
 
Thanks! I'll take a look.
Page Index Toggle Pages: 1