0

Im making a program to play pacman and have several classes, i have a Wall class, a Goast class a Pacman class a Pellot class and a Board class. all of them are compiled without errors except board, it is not complete but what i have is not compiling, what i have is

import java.awt.*;
import javax.swing.*;
public class Board{
    private int xDim_=1000;
    private int yDim_=900;
    private Pacman pacman_=new Pacman(475,525,0);
    private Goast greenredGoast_=new Goast(525,350,2,Color.RED);
    private Goast onageGoast_=new Goast(525,275,1, Color.ORANGE);
    private Goast pinkGoast_=new Goast(425,350,2,Color.MAGENTA);
    private Goast blueGoast_=new Goast(425,275,0,Color.CYAN);
    Wall[] walls = new Wall[22];
    walls[0]=new Wall(50,850,900,50);  
    walls[1]=new Wall(0,0,50,900);
    walls[2]=new Wall(50,0,900,50);
    walls[3]=new Wall(125,125,50,150);

im getting 100 errors that are on every entry in creating a new Wall, the errors are as follows:

 walls[0]=new Wall(50,850,900,50);  
          ^
Board.java:12: error: ';' expected
    walls[0]=new Wall(50,850,900,50);  
           ^
Board.java:12: error: illegal start of type
    walls[0]=new Wall(50,850,900,50);  
            ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
             ^
Board.java:12: error: ';' expected
    walls[0]=new Wall(50,850,900,50);  
                ^
Board.java:12: error: illegal start of type
    walls[0]=new Wall(50,850,900,50);  
                     ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                      ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                         ^
Board.java:12: error: illegal start of type
    walls[0]=new Wall(50,850,900,50);  
                            ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                             ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                                 ^
Board.java:12: error: illegal start of type
    walls[0]=new Wall(50,850,900,50);  
                                   ^
Board.java:12: error: <identifier> expected
    walls[0]=new Wall(50,850,900,50);  
                                    ^
Board.java:12: error: ';' expected
    walls[0]=new Wall(50,850,900,50);  
                                     ^

and im getting that for every single Wall. i cant figure out what is wrong someone PLEASE help!! thanks

1
  • 1
    What does the Wall class look like? Commented Nov 16, 2013 at 4:42

3 Answers 3

3

You can't invoke walls[0]=new Wall(50,850,900,50); at class level.

public class Board{
    ...
    Wall[] walls = new Wall[22];
    walls[0]=new Wall(50,850,900,50);
    walls[1]=new Wall(0,0,50,900);
    ...
}

You need to place it in either:

initialization block,

public class Board{
    ...
    Wall[] walls = new Wall[22];
    {
        walls[0]=new Wall(50,850,900,50);
        walls[1]=new Wall(0,0,50,900);
        ...
    }
    ...
}

constructor

public class Board{
    ...
    Wall[] walls = new Wall[22];

    public Board(){
        walls[0]=new Wall(50,850,900,50);
        walls[1]=new Wall(0,0,50,900);
        ...
    }
    ...
}

or some method

public class Board{
    ...
    Wall[] walls = new Wall[22];

    public void fillBoard(){
        walls[0]=new Wall(50,850,900,50);
        walls[1]=new Wall(0,0,50,900);
        ...
    }
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you only need one array of walls then I might use a static block to initialize my array of Wall(s) like this -

private static Wall[] walls = new Wall[22];

static {
  walls[0]=new Wall(50,850,900,50);  
  walls[1]=new Wall(0,0,50,900);
  walls[2]=new Wall(50,0,900,50);
  walls[3]=new Wall(125,125,50,150);
  ...
}

Comments

0

Create a method and put your array codes inside that the following code will work

import java.awt.*;
import javax.swing.*;
public class Board{
    private int xDim_=1000;
    private int yDim_=900;
    private Pacman pacman_=new Pacman(475,525,0);
    private Goast greenredGoast_=new Goast(525,350,2,Color.RED);
    private Goast onageGoast_=new Goast(525,275,1, Color.ORANGE);
    private Goast pinkGoast_=new Goast(425,350,2,Color.MAGENTA);
    private Goast blueGoast_=new Goast(425,275,0,Color.CYAN);

    public void run() { // create a method
    Wall[] walls = new Wall[22];
    walls[0]=new Wall(50,850,900,50);  
    walls[1]=new Wall(0,0,50,900);
    walls[2]=new Wall(50,0,900,50);
    walls[3]=new Wall(125,125,50,150);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.