• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange array problem

 
Parka Teoh
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create the simplest array and it doesn't work...

Here's the java file with the array code



It gives me the following error message..

D:\webapp_proj2\WEB-INF\classes\limited\Pack1.java:9: ']' expected
myDouble[0] = 21.2;
^
D:\webapp_proj2\WEB-INF\classes\limited\Pack1.java:9: <identifier> expected
myDouble[0] = 21.2;

I'm lost.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rikki Bostelmann
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider

class test {
int i = 0;
}

versus

class test {
int i;
i = 0;
}

The first compiles just fine, whereas in the second example the compiler complains "identifier required". The reason is the following:
classes have state and behavior. The state is given in the instance variables, which may be instantiated at once (1st example), and the behavior is given by methods.
The line i = 0; in the second example would never really be considered: it's not considered when constructing an instance of the class (only int i; till the ; is read), and is not in any method.
So the compiler doesn't know what to do with that line and assumes you forgot the identifier ("int" i = 0; or "char" i = 0; ) .

(Believe me, exactly this mistake took me a while when I first ran into it!)

All the best,
Rikki
[ October 18, 2004: Message edited by: Rikki Bostelmann ]
 
Parka Teoh
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks a lot!!!

I have created a constructor to read the values into the array.

Another problem came up, though.

This file is in a package.

When I use another file to reference the array index myDouble[0] and print out using the command prompt, the result was 0.0, instead of the 21.2 I read it into the first file (Pack1.java in this case)

Here's my other file called Pack2.java

 
Parka Teoh
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh okay, solved. THanks to all who replied.

I had to instantiate Pack1 as an object inside Pack2.

Man, fundamentals.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic