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]
# 1

All those statements have to be inside a method.

You can declare and initialize a member variable outside a method, but statements have to be inside a method.

class Foo {

List list = new ArrayList();

public static void main(String[] args) {

list.add(something);

etc.

}

}

jverda at 2007-7-12 8:56:23 > top of Java-index,Java Essentials,New To Java...
# 2

> 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

mtedonea at 2007-7-12 8:56:23 > top of Java-index,Java Essentials,New To Java...
# 3
> [code] > -jGRASP exec: javac -g N:\School\Java\Code\218B\HW> 6\Test.java> jGRASP is still around?
xiarcela at 2007-7-12 8:56:23 > top of Java-index,Java Essentials,New To Java...
# 4
TY guys..... a great big DUH for me eh? Sometimes I get so caught up in what I am doing the little details slip right by.And yes Jgrasp is still in use at least in the beginner class I am taking. In fact it was just updated to 1.8.6 - WoooHooo huh?LilWiz.
LilWiza at 2007-7-12 8:56:23 > top of Java-index,Java Essentials,New To Java...