• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiler "cannot find symbol" for my ArrayList

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
reply
    Bookmark Topic Watch Topic
  • New Topic