aspose file tools*
The moose likes Beginning Java and the fly likes Compiler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiler "cannot find symbol" for my ArrayList" Watch "Compiler "cannot find symbol" for my ArrayList" New topic
Author

Compiler "cannot find symbol" for my ArrayList

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Chaps,

I hope one of you guys can help me. I'm trying to learn some Java by making a little card program. I have one class called GamePlay which creates an array of Card objects called 'pack[]'. Then I shuffle the cards. What I was hoping to do next was to create some Player objects who would each have an ArrayList which would hold the cards that they've been dealt.

So, from main I create a new Player object called adam. I just want to check that an ArrayList exists for him at the moment, so I have tried to call the ArrayList to ask if it is empty (isEmpty) and then ask the system to print it out.

However, the compiler keeps on coming up with the following message:

cannot find symbol
symbol : variable hand
location : class Player
System.out.println(adam.hand.isEmpty());

the pointer is pointing to the dot after adam.

Basicaly I'd love to know if my Player class can hold an ArrayList for each player as I want it to, and also how I can initialize it. Also, if it is possible, is it ok to refer to the ArrayList using adam.hand.add(blah) etc?

Here's my GamePlay class:

import java.util.List;
import java.util.ArrayList;

public class GamePlay {

public static void main(String[] args) {

Card[] pack = new Card[52];

for(int i = 0; i < 52; i++) {
pack[i] = new Card();
if (i < 13) {
pack[i].suit = "Diamonds";
pack[i].cardValue = i;
pack[i].getCardName();
}
else if (i > 12 && i < 26) {
pack[i].suit = "Hearts";
pack[i].cardValue = i - 13;
pack[i].getCardName();
}
else if (i > 25 && i < 39) {
pack[i].suit = "Spades";
pack[i].cardValue = i - 26;
pack[i].getCardName();
}
else if (i > 38 && i < 52) {
pack[i].suit = "Clubs";
pack[i].cardValue = i - 39;
pack[i].getCardName();
}
} // end of for loop

packShuffle(pack);

System.out.println(pack[0].cardob + " of " + pack[0].suit);

Player adam = new Player();
adam.playerName= "Adam";
System.out.println(adam.playerName);
System.out.println(adam.hand.isEmpty());

} // end of main

//** Shuffling method

public static void packShuffle(Card[] a) {

for (int x = 0; x < a.length; x++) {

int k = (int) (Math.random() * (a.length-1));

Card temp = new Card();
temp = a[x];
a[x] = a[k];
a[k] = temp;
} // end of for loop

} // end of packShuffle()


} // end of class

and here's my Player class:

import java.util.List;
import java.util.ArrayList;

public class Player {

String playerName;
double cashtotal;

public void startHand() {
List<Card> hand = new ArrayList<Card>();
}

}

Thanks chaps! Sorry this is so long by the way.

Cheers

Joe


OCPJP 85%
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Joe,

Welcome to JavaRanch!

The declaration for "hand" is inside the method startHand(). That makes it a local varable. A local variable exists only while its method is running. You can't access a local variable with "object.variable", because the variable isn't a part of the object. If "hand" were declared out there next to "cashtotal" and "playerName", you would find that your code would work just fine.

But, that doesn't mean it would be good style. Generally a class should fiddle with its own data, and not with the data of other classes. Rather than calling adam.hand.isEmpty(), it would be better to give Player a method "isHandEmpty()" and call that; that method, in turn, could call isEmpty() on the hand variable. Likewise all your initializations of Cards ad Players should be done with constructors -- have you learned about those yet?


[Jess in Action][AskingGoodQuestions]
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Ernest

Thanks for replying so quickly!! (or at all for that matter)

I have now got rid of the method in the player object and just declared the 'hand' ArrayList along with the other variables and it works. Yippeeeee!!

However, in my main method, I try to add the first Card object of my 'pack' array (pack[0]), (ie I'm trying to 'deal' a card to the 'adam' Player object's 'hand' ArrayList. I don't get a compiler problem, but when I print out the adam.hand.isEmpty() thingy it says 'true', which I take to mean that the ArrayList IS empty and so I haven't managed to add the pack[0] at all. Harumpf!!

Also I'm just wondering if you'd know how I would ask the System to print out some of the values in the adam.hand ArrayList's new Card object (The one that should have been added, but wasn't). Would I say System.out.println(adam.hand(0).suit); ? How would I specify the index of the Card object?

Oh and no I haven't dealt with constructors yet. As you can tell I'm still at the walking-blindly-into-walls stage. (Thinks: Hmmm perhaps I should do another couple of chapters of that book....Naaaaa.)

Here's my new code:



And for the Player Object:


Thanks for your help!!

Cheers

Joe

[edit]Add code tags. CR[/edit]
[ October 25, 2008: Message edited by: Campbell Ritchie ]
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Woops, sorry actually I do now get 'false' returned by my isEmpty() call to the 'hand' ArrayList.

I'd still love to know the best way for addressing my Card objects inside the 'hand' ArrayList, so I can get values out of them. Could I use 'adam.hand(0).suit' for example to get the value of the 'suit' variable of the Card object which is at index(0) of the 'hand' ArrayList?

Cheers guys.

Joe
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Originally posted by Joe Lemmer:
Could I use 'adam.hand(0).suit' for example to get the value of the 'suit' variable of the Card object which is at index(0) of the 'hand' ArrayList?
Yes and no.

You will get at the suit like that, but only if "suit" has non-private access. Since fields ought to have private access this is a bit of iffy design akin to what EFH mentioned. If you need to get the suit from a card at position i in the List, your Hand class should have methods like getCardAt(int i) and getSuitFromCardAt(int i).

Getting the right answer to isEmpty() should be easy if you find what methods there are in the List interface.

And please indent your code 4 spaces at a time and use the code button. I have edited your post a bit so you can see how much better it looks.
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Thanks Campbell.

I've taken on board what you said about the fact that 'suit' should be private and accessed by a method in the same class - Encapsulation right?

So I'll work on that.

Cheers for your reply and to Ernest. So long.

Joe

Giddee up now Frank...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You're welcome
 
 
subject: Compiler "cannot find symbol" for my ArrayList