15

I'm trying to create some kind of list to store values from the array 'table'. (I'm using a arraylist here, but should I be using a list instead?) However, every time I try to compile, it throws the following error:

cannot find symbol symbol : class ArrayList location: class players.TablePlayer

The code is below.

public class TablePlayer extends Player {

    int[][] table;
    ArrayList goodMoves;


    public TablePlayer(String name) {
        super(name);
    }

    @Override
    public int move() {
        int oppLast = opponentLastMove();
        int myLast = myLastMove();
        if (!isLegalMove(oppLast)) {
            return 0; // temporary
        }
        if (wonLast()) {
            table[oppLast][myLast] = 1;
            table[myLast][oppLast] = -1;
        }
        if ((wonLast() == false) && (oppLast != myLast)) {
            table[oppLast][myLast] = -1;
            table[myLast][oppLast] = 1;
        }
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table.length; j++) {
                if (table[i][j] == 1) {
                    goodMoves.add(table[i][j]);
                }
            }
        }

        return oppLast; // temporary
    }

    @Override
    public void start() {
        int[][] table = new int[7][7];
        ArrayList<int> goodMoves = new ArrayList<int>();
    }
}

Any help would be great, thanks!

2
  • Sounds like you're just missing the import. Apr 3, 2013 at 13:57
  • A decent Java IDE would help you with that.
    – maba
    Apr 3, 2013 at 14:10

4 Answers 4

33

Do you have an import statement in the top of the file?

import java.util.ArrayList;
3
  • Thanks, that solved the errors, but it's thrown a new error. warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
    – csstudent
    Apr 3, 2013 at 13:58
  • Using an ArrayList<Integer> instead of an ArrayList should remove that warning. Apr 3, 2013 at 14:01
  • 1
    I added in the 'Integer' generic, but still throwing the same error.
    – csstudent
    Apr 3, 2013 at 14:09
2

While doing any java program just

import java.util.*;

Because * will import all the packages from util.

And all the basic package are present in that java.util like Scanner, ArrayList, etc...

So to avoid errors first check you have imported that.

0

Before use a class, you need to import it into you class file definition.

Add it on top of your file:

import java.util.ArrayList;

For more info about imports, look it up here

It is recommended to learn how to use a IDE, like Eclipse, Netbeans. It will help you with these common mistakes when we are programming in Java (in this case) outside a integrated environment.

2
  • Yeah, I'm trying using Dr. Java right now, but having some trouble with it.
    – csstudent
    Apr 3, 2013 at 14:10
  • I recommend Eclipse IDE. Try some tutorial to start things up. Apr 3, 2013 at 14:13
-4

I am new to the community and on a path of learning now. But I think the main problem is of int

Wrong

ArrayList <int> goodMoves = new ArrayList <int>();

Right

ArrayList <Integer> goodMoves = new ArrayList <Integer>();
1
  • 1
    This is an error ("unexpected type"), but it is not the reason for the error "cannot find symbol". Aug 3, 2022 at 14:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.