Search code examples
javaidentifier

Why do I get <identifier> expected?


The following code:

import java.util.ArrayList;
import java.awt.Point;
public class Polygon{
    ArrayList<Point> points;

    //constructs polygon without points

    public Polygon() {
        points = new ArrayList<Point>();
    }


    /*
     * adds a point to points
     */
    public void addPoint(Point){
        points.add(Point);
    }

    public void draw(){
        for(int i = 0; i < points.size(); i++){
             if(i == 0){
                 points.get(0).draw()
             }else{
                 points.get(i).draw()
                 Line line = new Line(points.get(i-1).getX(), points.get(i-1).getY(), points.get(i).getX(), points.get(i).getY());
                 line.draw();                 
             }
        }

        if(points.size() >= 2){
            Line line = new Line(points.get(-1).getX(), points.get(-1).getY(), points.get(-2).getX(), points.get(-2).getY());
            line.draw();
        }
    }


}

gives the exception message:

<identifier> expected for public void addPoint(Point)

I just cannot figure out why? Those identifiers are normally needed to tell an ArrayList which objects it is going to hold, right?


Solution

  • You are missing the identifier. Point is a type.

    public void addPoint(Point p){
            points.add(p);
        }