Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multidimensional array problem

 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I've written this little piece of code:



but when I try and compile, it gives me this error:

SetUpGame.java:5: ']' expected
board[4][0] = 5;
^
1 error


For the life of me, I cannot figure out what it wants me to do here. If I remove the "4", then how can I tell it what value I'm trying to change to "5"?

Confused,
Mike
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

A statement like "board[4][0] = 5;" needs to be inside a method; you've put it in the class body, where statements are illegal syntax. All you can put in a class body are variable declarations, with or without initializers.

You might want to put your assignment statement into a constructor for this class.
 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! Yes! Thank you very much!

So simple, yet so completely overlooked by me.

Many thanks,
Mike
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. If you ever teach Java to students, you learn to look for this mistake fast. The other similar one is declaring a local variable "public" (for example.) The error message also makes no sense and it's very hard to see what's wrong!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic