2

I'm new to Java, and I'm getting an error in my main function when I try to create an instance of a DeckofCards class. The DeckofCards class is supposed to be a private array of "Cards" objects. I think my issue is something in the DeckofCards class, for some reason it's not an array I think? Maybe I created it wrong?

The errors in my main are '(' or '[' expected and array required but DeckofCards found

Here is my main function:

public static void main(String[] args) {

        Card myCard = new Card(13,1);
        System.out.println(myCard.getSuit());
        System.out.println(myCard);
        DeckofCards myDeck = new DeckofCards;  //error here
        for(int i=0; i<53; i++) {
            System.out.println(myDeck[i]); //second error here
        }
    }

Here is my DeckofCards class:

public class DeckofCards {

    private Card[] deck = new Card[52];

    public DeckofCards(){
        int i = 0;

            for(int s = 1; s<5; s++){
                for(int r = 1; r<14; r++){
            deck[i].rank = r;
            deck[i].suit = s;
            i++;

    }
}
    }
}

If anyone can tell me if I'm missing some syntax or something that'd be great! Thank you.

4
  • "for some reason it's not an array I think" - Well you just about hit the nail on the head. Use methods to interact with the Card array in your deck class. Commented Apr 20, 2014 at 22:51
  • 1
    Instead of new DeckofCards use new DeckofCards() Commented Apr 20, 2014 at 22:52
  • 1
    Note: to construct an object (or actually set its parameters) you need to call a constructor. A constructor is a method of your class (even if you do not write your own constructor explicitely, Java will provide a default constructor for the class.) Now you actually wrote your constructor (public DeckOfCards()) in your class, and see: it is a method. To call a method in Java you need to type methodName(arguments). The parentheses are obligatory and arguments are not (for example, the constructor you wrote takes no arguments). Commented Apr 20, 2014 at 22:58
  • Can someone explain why System.out.println(myDeck[i]) returns an error? System.out.println(myCard) returns the card ("Ace of Spades" for example) and isn't myDeck[i] just a card? Commented Apr 21, 2014 at 1:04

4 Answers 4

5

You need to call the constructor:

DeckofCards myDeck = new DeckofCards(); // note parens!

In Java, the parentheses are required.

In the constructor, you will also need to initialize each element of the array to a new Card object:

for(int s = 1; s<5; s++){
    for(int r = 1; r<14; r++){
        deck[i] = new Card();
        deck[i].rank = r;
        deck[i].suit = s;
        i++;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Ted! Can you also explain why System.out.println(myDeck[i]) returns an error? println(myCard) prints out the card value ('Four of Clubs") so shouldn't that last for loop print out the values of all the cards?
@Acoustic77 - The variable myDeck is not an array, so you can't access parts of it using array subscripting. If you want to print the i-th card in the deck, you'll need a getter method in DeckofCards (for instance: public Card getCard(int i) { return deck[i]; }). Then you could try System.out.println(myDeck.get(i));.
3

Replace

DeckofCards myDeck = new DeckofCards;

with

DeckofCards myDeck = new DeckofCards();

and myDeck was never initialized to be an array.

Comments

3

The error is pretty clear, myDeck is a single custom Object rather than an array

DeckofCards myDeck = new DeckofCards();  // parenthesis here
for (int i=0; i<53; i++) {
    System.out.println(myDeck); // no brackets here
}

Although the loop itself should be located within the toString method of the DeckofCards class.

Comments

1

In order to iterate through the DeckofCards you'll want to expose that private Card[] array. You can use the bean notation getCards() or just make the array public.

    public class DeckofCards {

        private Card[] deck = new Card[52];

        public DeckofCards(){
            int i = 0;

                for(int s = 1; s<5; s++){
                    for(int r = 1; r<14; r++){
                deck[i].rank = r;
                deck[i].suit = s;
                i++;

        }
      } 
    }
     public Card[] getCards(){

          return deck;

     }
   }

I would probably just make the deck public.

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.