• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Simple Array Question  RSS feed

 
Ranch Hand
Posts: 43
  • 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;
}
__________________________________________________________________________
 
Sheriff
Posts: 7023
  • 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
  • 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
 
Ranch Hand
Posts: 84
  • 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
  • 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?
 
Ranch Hand
Posts: 268
  • 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
  • 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
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!