Jump to content
  • Advertisement
Sign in to follow this  
Aezon

Java Compile Error. "[ Expected"

This topic is 3082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I copied this code from "Killer Game Programming in Java": Point2D.Double incrs[] = new Point2D.Double[NUMDIRS]; incrs[N] = new Point2D.Double(0.0,-1.0); incrs[NE] = new Point2D.Double(0.7,0.7); incrs[E] = new Point2D.Double(1.0,0.0); incrs[SE] = new Point2D.Double(0.7,-0.7); incrs[S] = new Point2D.Double(0.0,-1.0); incrs[SW] = new Point2D.Double(-0.7,0.7); incrs[W] = new Point2D.Double(-1.0,0.0); incrs[NW] = new Point2D.Double(-0.7,-0.7); ...and for each line after the first, it gives the error: "[ Expected", I checked for a syntax error and couldn't find one, can someone please help me? (ps. This is used to store Point2D interpretations of the eight compass directions.)

Share this post


Link to post
Share on other sites
Advertisement
From googling, it appears that Java is like C#; when you declare an array, the brackets go with the type, not with the variable, like so:

Point2D.Double[] incrs = new Point2D.Double[NUMDIRS];

Share this post


Link to post
Share on other sites
Quote:
Original post by Nypyren
From googling, it appears that Java is like C#; when you declare an array, the brackets go with the type, not with the variable, like so:

Point2D.Double[] incrs = new Point2D.Double[NUMDIRS];


In java it doesn't matter if you put the brackets infront or behind the variable name.

What about N, NE, E etc. Are these variables ints ? Are they defined in the class as static variables or in the method ? If you have defined them in an other class as static variables, you need to add an static import.

Share this post


Link to post
Share on other sites
Hi,

just copied your code and tried to compile it in a simple hello world application. It seems to compile just fine, no matter how I define the variables NUMDIRS, N, ... (be it local, field, static, or final)

What I would suggest is trying to comment parts of the code to identify which lines are actually causing the compiler error. That is, if you're not using an IDE of some sorts (e.g. Eclipse or Intellij). If you're not using one, it's highly recommended that you do. This will require some extra effort when starting out, but in the long run, you'll benefit a lot from it.

Share this post


Link to post
Share on other sites
(Sorry for the late post, the forums were giving me problems)


I commented out parts of the code, and determined that it happens with each line after the first one.

NUMDIRS and each index(N,NE,NW, etc) are all private static final ints.

NUMDIRS = 8, and each other int equals it's respective index in the array(0,then 1, then 2, etc.)

Everything is declared and initialized in the same class, the only method is main(String args[]) which is empty.

The only import is java.awt.*;

Share this post


Link to post
Share on other sites
Quote:
Everything is declared and initialized in the same class, the only method is main(String args[]) which is empty.


And is this code in a method?

If I put that code into a method, it compiles fine, but if I put it out into the class definition, it balks. It doesn't give me the same error you do, but its a similarly dumbfounding syntax error along the lines of "something is here and I expected something different".

Move it to the constructor.

I'm not totally sure what you're doing but if these are intended as constants, consider this:


final static int NUMDIRS = 8;
final static int N = 0, NE = 1, E = 2, SE = 3, S = 4, SW = 5, W = 6, NW = 7;

final static Point2D.Double[] incrs;

static {
incrs = new Point2D.Double[5];

incrs[N] = new Point2D.Double(0.0,-1.0);
incrs[NE] = new Point2D.Double(0.7,0.7);
incrs[E] = new Point2D.Double(1.0,0.0);
incrs[SE] = new Point2D.Double(0.7,-0.7);
incrs[S] = new Point2D.Double(0.0,-1.0);
incrs[SW] = new Point2D.Double(-0.7,0.7);
incrs[W] = new Point2D.Double(-1.0,0.0);
incrs[NW] = new Point2D.Double(-0.7,-0.7);
}


That's a "static constructor" and lets you use code (beyond simple initializers) to setup your static members on load.

Share this post


Link to post
Share on other sites
Thanks for that, I re-did the code that way and it compiled.

The app is not yet functional, though, so I will post any more errors I run into here.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!