d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Creating A Deck Of 52 Cards In Java > Iso Help
Add Reply New Topic New Poll
Member
Posts: 8,324
Joined: Jul 8 2009
Gold: 128.00
Nov 28 2012 08:27pm
Hey all first time posting in these forums and I hope this is where I can ask for help?

Basically I have to create a deck of cards using java making a deck from a seperate class that makes each individual card.
So far I have the card class up and working with a toString method making a card given parameters (1,1) pop up with "Ace of Spades" same with other parems for jack queen king and so on.

What I'm trying to do with the code below is create an array called deck being able to hold objects of type card and create 52 of them.

Sooooo when I go to see if it's working with a simple System.out.println(deck[1]); statement all I get is ann error highlighting the "[1]" and saying array required, but deckofcards found?


import java.util.*;

public class DeckOfCards
{

public Card[] deck = new Card[52];
public int i = 0;

public DeckOfCards()
{
int a = 1;
int b = 1;
if (i < deck.length)
{
Card card = new Card(a,b);
deck[i] = card;
i++;
if (a==13)
{
a = 1;
b++;
}
else
a++;
}
}
}


Please help!
Hopefully that's easy to understand but I know with me I have no idea how to read other peoples code and see what's wrong I need someone to explain to me exactly what's happing so if you need any other info ask.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 28 2012 08:38pm
i dont see the println anywhere. i'm guessing you're doing something like this:

DeckOfCards deck = new DeckOfCards();
System.out.println(deck[1]);

in this case, deck is of type DeckOfCards which clearly isn't an array. you can do deck[1] inside the DeckOfCards class just fine.

to fix it, add a get(i) method inside DeckOfCards and call deck.get(1)

ps: dont use variables named a, b. use rank, suit instead. you probably also want to use a loop instead of "if" when creating the deck.

This post was edited by carteblanche on Nov 28 2012 08:39pm
Member
Posts: 8,324
Joined: Jul 8 2009
Gold: 128.00
Nov 28 2012 08:40pm
Quote (carteblanche @ Nov 28 2012 06:38pm)
i dont see the println anywhere. i'm guessing you're doing something like this:

DeckOfCards deck = new DeckOfCards();
System.out.println(deck[1]);

in this case, deck is of type DeckOfCards which clearly isn't an array. you can do deck[1] inside the DeckOfCards class just fine.

to fix it, add a get(i) method inside DeckOfCards and call deck.get(1)

ps: dont use variables named a, b. use rank, suit instead



Thank-you very much for the quick response and critique I'll fix'er up.
Member
Posts: 8,324
Joined: Jul 8 2009
Gold: 128.00
Nov 28 2012 08:56pm
Member
Posts: 30,717
Joined: Sep 19 2007
Gold: 254.00
Nov 28 2012 09:07pm
a cooler way of generating the 52 is:

for(int x = 0; x < 52; x++)
{
rank = x%13;
suit = x/13;
}
Member
Posts: 632
Joined: Apr 2 2010
Gold: 50.00
Nov 28 2012 10:43pm
Quote (J_B @ Nov 28 2012 10:07pm)
a cooler way of generating the 52 is:

for(int x = 0; x < 52; x++)
{
  rank = x%13;
  suit = x/13;
}


rank = x % 13 + 1;

for his program because he does not rank his cards starting with 0.

This post was edited by BaghdadAssUp on Nov 28 2012 10:47pm
Member
Posts: 16,218
Joined: Sep 27 2009
Gold: 13.00
Nov 29 2012 03:55pm
Why are u using a [ ] for this ;;

This post was edited by Bremen on Nov 29 2012 03:55pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll