aspose file tools*
The moose likes Beginning Java and the fly likes multidimensional array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multidimensional array problem" Watch "multidimensional array problem" New topic
Author

multidimensional array problem

MR Chen
Ranch Hand

Joined: Aug 10, 2007
Posts: 31
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
MR Chen
Ranch Hand

Joined: Aug 10, 2007
Posts: 31
Aha! Yes! Thank you very much!

So simple, yet so completely overlooked by me.

Many thanks,
Mike
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multidimensional array problem