ArrayList help
I am probably missing something really stupid but when I use this code:
publicclass Test
{
//Declare Person objects and add to collection
ArrayList personCollection=new ArrayList();
Person testObj1=new Person("Rusty","Matteson","326 North Ave.","Rochester","NY",14612);
Person testObj2=new Person("Margy","Smith","123 Lowden Point Rd.","Hershey","PA",98765);
Person testObj3=new Person("Mike","Couyle","456 Peach Blossom Dr.","Hilton","NY",14468);
personCollection.add(testObj1);
personCollection.add(testObj2);
personCollection.add(testObj3);
}
I get these errors...
-jGRASP exec: javac -g N:\School\Java\Code\218B\HW 6\Test.java
Test.java:8: <identifier> expected
personCollection.add(testObj1);
^
Test.java:9: <identifier> expected
personCollection.add(testObj2);
^
Test.java:10: <identifier> expected
personCollection.add(testObj3);
^
3 errors
...and do not know why.
Any help would be greatly appreciated.
[1746 byte] By [
LilWiza] at [2007-11-27 3:52:20]

> I am probably missing something really stupid but
> when I use this code:
>
> > public class Test
> {
> //Declare Person objects and add to collection
> ArrayList personCollection=new ArrayList();
> Person testObj1= new Person("Rusty","Matteson","326
> 6 North Ave.","Rochester","NY",14612);
> Person testObj2= new Person("Margy","Smith","123
> 3 Lowden Point Rd.","Hershey","PA",98765);
> Person testObj3= new Person("Mike","Couyle","456
> 6 Peach Blossom Dr.","Hilton","NY",14468);
> personCollection.add(testObj1);
> personCollection.add(testObj2);
> personCollection.add(testObj3);
> }
>
>
>
>
> I get these errors...
>
> [code]
> -jGRASP exec: javac -g N:\School\Java\Code\218B\HW
> 6\Test.java
>
> Test.java:8: <identifier> expected
> personCollection.add(testObj1);
>^
> pected
> personCollection.add(testObj2);
>^
> xpected
> personCollection.add(testObj3);
>^
> not know why.
>
> Any help would be greatly appreciated.
The logic contained in personCollection.add() (method invocation expression) must be contained within a method, since it's not an instance varilable initializer (which would be allowed). Try to create an instance method and move the three invocation of add() there and your errors will disappear.
Message was edited by:
mtedone