2

I'm running into a problem when trying to create an ArrayList in Java, but more specifically when trying to add() to it. I get syntax errors on the people.add(joe); line...

Error: misplaced construct: VariableDeclaratorId expected after this token.
    at people.add(joe);
                  ^

It's my understanding that an ArrayList would be better than an array for my purposes, so my question is, is that the case and if not, where am I going wrong with my syntax?

This is my code...

import java.util.ArrayList;

public class Person {
    static String name;
    static double age;
    static double height;
    static double weight;

    Person(String name, double age, double height, double weight){
        Person.name = name;
        Person.age = age;
        Person.height = height;
        Person.weight = weight;
    }

    Person joe = new Person("Joe", 30, 70, 180);
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(joe);
}
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Jarod
  • 157
  • 2
  • 4
  • 12
  • 2
    You need to add things within a block (method, constructor, initializer, etc). – Beau Grantham Jun 20 '12 at 00:33
  • 2
    btw, you're misusing static = all of your People will share the same name, age, etc etc. you want to remove the static keyword and replace "Person.name = " with "this.name=" in the constructor. – Steve B. Jun 20 '12 at 00:36
  • Thank you for all the help. It is working now within a method. – Jarod Jun 20 '12 at 00:50

7 Answers7

8
static String name;      
static double age;
static double height;
static double weight;

Why are these variables defined as static?

It looks like you are doing it in the Person class. Doing it in the class is OK(it can be done), but doesn't make much sense if you are creating an ArrayList of Person objects.

The main point here is that this must be done within an actual method or constructor or something (an actual code block). Again, I am not entirely sure how useful an ArrayList of type Person would be inside of a Person class.

import java.util.ArrayList;

public class Person 
{                   // Don't use static here unless you want all of your Person 
                    // objects to have the same data
   String name;
   double age;
   double height;
   double weight;

   public Person(String name, double age, double height, double weight)
   {
      this.name = name;       // Must refer to instance variables that have
      this.age = age;         // the same name as constructor parameters
      this.height = height;    // with the "this" keyword. Can't use 
      this.weight = weight;    // Classname.variable with non-static variables
   }

}

public AnotherClass 
{
   public void someMethod()
   {
      Person joe = new Person("Joe", 30, 70, 180);
      ArrayList<Person> people = new ArrayList<Person>();
      people.add(joe);
      Person steve = new Person("Steve", 28, 70, 170);
      people.add(steve);            // Now Steve and Joe are two separate objects 
                                    // that have their own instance variables
                                    // (non-static)
   }
}
Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
0

Put that code in a main method like:

 public class Person {

     public static void main(String[] args ) {
         Person joe = new Person("Joe", 30, 70, 180);
         ArrayList<Person> people = new ArrayList<Person>();
         people.add(joe);
     }
 }
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
0

write these inside some method or block.
like::

import java.util.ArrayList;

public class Person 
{
   static String name;
   static double age;
   static double height;
   static double weight;

  Person(String name, double age, double height, double weight)
  {
    Person.name = name;
    Person.age = age;
    Person.height = height;
    Person.weight = weight;
  }

  public static void main(String args[])
  {
    Person joe = new Person("Joe", 30, 70, 180);
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(joe);
  }
}
Eight
  • 4,194
  • 5
  • 30
  • 51
0

ArrayList add operation should be done in method block (maybe in main) as others suggested.

public class Person  {
  static String name;
  static double age;
  static double height;
  static double weight;

  Person(String name, double age, double height, double weight) {
    Person.name = name;
    Person.age = age;
    Person.height = height;
    Person.weight = weight;
  }
}

public static void main(String[] args) {
  Person joe = new Person("Joe", 30, 70, 180);
  ArrayList<Person> people = new ArrayList<Person>();
  people.add(joe);
}
Meow
  • 18,371
  • 52
  • 136
  • 180
0

Place your code (after the constructor) in a method then call the method somewhere else. Your fields must not be static for this purpose since all instances will share it.

setzamora
  • 3,560
  • 6
  • 34
  • 48
0

First, your ArrayList variable really should be at an instance level, or static. Declare it as such:

private ArrayList<Person> people;

or:

static ArrayList<Person> people;

Second, you require some sort of function to perform operations in.

public static void addPerson(Person p) {
    people.add(p);
}

Third, you need to invoke it. You can do this by:

Person.addPerson(new Person("Joe", 30, 70, 180));

in the body of main(), or somewhere that's relevant to your program's execution.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Put curly braces around people.add(joe); and the code will compile:

{people.add(joe);}

Why? Leave as an exercise on initialization blocks.

Yevgen Yampolskiy
  • 7,022
  • 3
  • 26
  • 23