Java Compile Error. "[ Expected"

Started by
8 comments, last by Aezon 13 years, 12 months ago
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 = 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.)
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!
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];
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.

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.
(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.*;
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!
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 = new Point2D.Double(0.0,-1.0);<br>		incrs[SW] = new Point2D.Double(-0.7,0.7);<br>		incrs[W] = new Point2D.Double(-1.0,0.0);<br>		incrs[NW] = new Point2D.Double(-0.7,-0.7);<br>	}</pre><br><br>That's a "static constructor" and lets you use code (beyond simple initializers) to setup your static members &#111;n load.
Thanks for that, I re-did the code that way and it compiled.
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!
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.
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!
Tizight. Do I get a rating++?
Ooh! New button.(Testament to how unobservant I am)

Sure. ^.^
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!

This topic is closed to new replies.

Advertisement