File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Array Question

 
Frankie Chee
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
when I compile the following code I receive an error saying <identifier> expected and cannot resolve symbol.
What could be the problem?
Below is the code:
__________________________________________________________________________

class array1
{
int bok[];
bok[]=new int[12]
bok[0]=1;
}
__________________________________________________________________________
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're missing a semicolon at the end of one of the lines.
 
Frankie Chee
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sheriff,
Glad to see you around
I put in the semi-colon but it prompt me the following error:
______________________________________________________________________
D:\practice\array1.java:7: <identifier> expected
bok[]=new int[12];
^
D:\practice\array1.java:9: ']' expected
bok[0]=1;
^
D:\practice\array1.java:9: <identifier> expected
bok[0]=1;
^
D:\practice\array1.java:7: cannot resolve symbol
symbol : class bok
location: class array1
bok[]=new int[12];
^
D:\practice\array1.java:9: cannot resolve symbol
symbol : class bok
location: class array1
bok[0]=1;
^
5 errors
Tool completed with exit code 1
 
Carlos Failde
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare and instantiate in one line outside of a method, so

but you can not instantiate on a separate line like you are trying

The compiler was looking for an identifier to start the second line and that's responsible for one of the compilation errors.

You can have a block of code which can be used to set up variables:

Note the braces. This code is automatically executed before any main method.
Writing the line without the braces makes the compiler look for a varaible declaration, which is why when it sees the [ after 'bok' it thinks you're declaring an array and looks for the matching ] but the 0 inbetween leads to the " ']' expected " error pointing at 0.
 
Frankie Chee
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carlos,
thanks for your explanation. Very glad you are teaching me things that are not found in most textbooks.(I feel so privilege )
I understand what you mean that after '[', JVM is looking for another '[' because its thinking I am trying in declare int bok[], right?
What does enclosing 'bok[0]=1' with curly braces mean to the JVM? I don't quite understand how executing it before any main method will eradicate the errors which occur previous. Could you explain just a bit more?
 
sever oon
Ranch Hand
Posts: 268
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carlos lost me on that last comment...I see no need for the braces.
Here are all the legal ways (I think I got 'em all, anyway) to declare an array:

sev
 
Carlos Failde
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*snip*
[ March 01, 2004: Message edited by: Carlos Failde ]
 
Carlos Failde
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frankie Chee:

I understand what you mean that after '[', JVM is looking for another '[' because its thinking I am trying in declare int bok[], right?

Correct.

What does enclosing 'bok[0]=1' with curly braces mean to the JVM? I don't quite understand how executing it before any main method will eradicate the errors which occur previous. Could you explain just a bit more?

There was nothing wrong with the line, it's just that the compiler objected to where you put it. If you had put it inside a method or a constructor, then there would have been no compile errors. But putting it on a line by itself is disallowed.
Blocks of code surrounded by {} are executed when a class is first accessed, before any methods of that class are. How about giving bok[0] a random value?:


Originally posted by sever oon:

Carlos lost me on that last comment...I see no need for the braces.
Here are all the legal ways (I think I got 'em all, anyway) to declare an array


Nice example, I think I'll keep it.
I note that all of those ways where inside a method. What Frankie was attempting was outside of a method which is why I suggested the braces.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic