Reputation: 1471
I'm building a little deck of cards class using an ArrayList. My problem I'm running into is that when I try to invoke my Deck method to populate the list, I'm getting a Cannot Find Symbol. Code below.
package deckofcards;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Deck{
ArrayList<String> al = new ArrayList<String>();
//test method
public void main(String[] args){
Scanner input = new Scanner(System.in);
int choice = 9;
al.Deck(); //<--Right here is the problem, it gives Cannot Find Symbol
//Symbol: method Deck() Location: variable al of type
//ArrayList<String>
while(choice != 0){
System.out.println("Shuffle - 1 Display - 2 Exit - 0:");
//didn't finish since it gave an error
}
}
private void Shuffle(){
Collections.shuffle(al);
}
private void Deck(){
al.add(0, "Ace of Spades");
al.add(1, "Two of Spades");
al.add(2, "Three of Spades");
//yadaa yadaa yadaa
I simply cannot figure out how to properly invoke my methods on the list. Does anyone know what I'm doing wrong?
NOTE: this is a secondary class file, the main class file of the package will simply contain the main test method.
Upvotes: 0
Views: 1203
Reputation: 4843
Very quick primer on Java Object-oriented programming (beginner level advice). Create classes that represent real world things. You will often find the Classes you want to create will be one of four archetypes:
Then for each class build methods that do something with the object (e.g. shuffle a deck).
For your situation, you have archetype 4 a Deck. It would appear that a Deck is made up of Cards (another class), and that you would want to shuffle a deck and get the next card from a deck. A Card would have some attributes, suit and value.
Continue down this path to create your classes. In your "main" class you would want to do things with objects of the classes you have created. You will want to ask yourself what a class should know. (Should Deck always be a 52-card, 4-suit AKQJ...2; or should you create a StandardDeck extending Deck and having those characteristics. Other decks might be a Bid Whist deck (extra jokers), a Spades Deck, a Canasta Deck, a Samba Deck, etc.)
Upvotes: 0
Reputation: 1471
I don't know how to declare a comment as an answer, but "yes. Deck is not a method of ArrayList, it is a method of Deck (btw, same name for class and method is very very error prone. don't do it). Why don't you just call Deck()? – njzk2 14 mins ago" was the perfect fix, thank you very much!
Upvotes: 0
Reputation: 354
The method named Deck() is the constructor for your class. In your main method, you create an instance of Deck with something like:
Deck deck = new Deck();
At the top of your Deck class, you have:
ArrayList<String> al = new ArrayList<String>();
This means that each instance of Deck will have an ArrayList member called al that is an initially empty ArrayList for holding Strings.
When a Deck is instantiated by using the constructor, the code in the constructor runs and the ArrayList gets populated with "Ace of Spades", "Two of Spades"....
The main method is a static method. That means that you can not directly access the ArrayList named al. To access it, you must go through an instance of Deck. For example if you want to see the cards in the deck, you must instantiate Deck and then access the array list as deck.al. Typically you don't want to access the member fields directly from main. You would want to create methods like shuffle() and deal() that each use the member al as their internal data.
Upvotes: 1
Reputation: 623
al doesn't have a Deck method since it is an ArrayList. You can call Deck() passing al as argument (e.g. Deck(al)) or else you may call Deck() and since al is in the same class you won't have any problem. However in the second case al must be a field of the Deck class, otherwise deck method won't find al symbol.
Upvotes: 1
Reputation: 85779
al
is an instance of ArrayList
, and ArrayList
class doesn't have a Deck
method. What you should do is to declare and initialize a variable of type Deck
which has a Deck
method:
//al.Deck();
Deck deck = new Deck();
deck.Deck(); //technically, this will do
But seems that you want to initialize the elements in the al
variable in Deck
class constructor, which you currently don't have. A constructor is a method that has the same name of the class and doesn't have any return type. So this method:
public void Deck() {
...
}
Is not a constructor. Just remove the void
keyword and it will become a constructor:
public Deck() {
...
}
So, after you declared a real constructor, then there's no Deck
method anymore, and your code will only be:
//al.Deck();
Deck deck = new Deck();
//deck.Deck(); //no need for this line anymore
Upvotes: 5