File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Strange array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange array problem" Watch "Strange array problem" New topic
Author

Strange array problem

Parka Teoh
Ranch Hand

Joined: Aug 28, 2004
Posts: 40
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.


Do you have a parka?<br /> <br /><a href="http://sg.geocities.com/inschooool" target="_blank" rel="nofollow">Visit my homepage</a> | <a href="http://parka.deviantart.com/" target="_blank" rel="nofollow">Parka Deviantart gallery</a>
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Rikki Bostelmann
Greenhorn

Joined: Sep 25, 2004
Posts: 8
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

Joined: Aug 28, 2004
Posts: 40
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

Joined: Aug 28, 2004
Posts: 40
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
 
subject: Strange array problem