Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HELP! "array required, but object found" :S

843785Oct 19 2008 — edited Oct 19 2008
Halo I'm new to Java and on one of my exercises I'm getting the following compile error and don't know why :S
"array required but Deck found"

Here is the code:


//Card.java this defines the Card class and object
public class Card {
private final int rank;
private final int suit;

// Kinds of suits
public final static int DIAMONDS = 1;
public final static int CLUBS = 2;
public final static int HEARTS = 3;
public final static int SPADES = 4;

// Kinds of ranks
public final static int ACE = 1;
public final static int DEUCE = 2;
public final static int THREE = 3;
public final static int FOUR = 4;
public final static int FIVE = 5;
public final static int SIX = 6;
public final static int SEVEN = 7;
public final static int EIGHT = 8;
public final static int NINE = 9;
public final static int TEN = 10;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;

public Card(int rank, int suit) {
assert isValidRank(rank);
assert isValidSuit(suit);
this.rank = rank;
this.suit = suit;
}

public int getSuit() {
return suit;
}

public int getRank() {
return rank;
}

public static boolean isValidRank(int rank) {
return ACE <= rank && rank <= KING;
}

public static boolean isValidSuit(int suit) {
return DIAMONDS <= suit && suit <= SPADES;
}

public static String rankToString(int rank) {
switch (rank) {
case ACE:
return "Ace";
case DEUCE:
return "Deuce";
case THREE:
return "Three";
case FOUR:
return "Four";
case FIVE:
return "Five";
case SIX:
return "Six";
case SEVEN:
return "Seven";
case EIGHT:
return "Eight";
case NINE:
return "Nine";
case TEN:
return "Ten";
case JACK:
return "Jack";
case QUEEN:
return "Queen";
case KING:
return "King";
default:
return null;
}
}

public static String suitToString(int suit) {
switch (suit) {
case DIAMONDS:
return "Diamonds";
case CLUBS:
return "Clubs";
case HEARTS:
return "Hearts";
case SPADES:
return "Spades";
default:
return null;
}
}

}
}


// Deck.java creates a deck of cards:P
import java.util.*;

public class Deck {

public static int numSuits = 4;
public static int numRanks = 13;
public static int numCards = numSuits * numRanks;

public Card[][] cards;

public Deck() {
cards = new Card[numSuits][numRanks];
for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
for (int rank = Card.ACE; rank <= Card.KING; rank++) {
cards[suit-1][rank-1] = new Card(rank, suit);
}
}
}

public Card getCard(int suit, int rank) {
return cards[suit-1][rank-1];
}
}


//AND finally, DisplayDeck.java that creates a deck of cards and display the cards of a deck:P
import java.util.*;

public class DisplayDeck {
public static void main(String[] args) {
Deck bobbysDeck = new Deck();
for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
for (int rank = Card.ACE; rank <= Card.KING; rank++) {
System.out.format("%s of %s%n",
Card.rankToString(bobbysDeck[suit-1][rank-1].getrank()), //<---here is where I get the error
Card.suitToString(bobbysDeck[suit-1][rank-1].getsuit())); // <----and here too

}
}
}
}

bobbysDeck is an array, so why is it giving me this compile error? Thank you very much for your help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 16 2008
Added on Oct 19 2008
19 comments
2,884 views