BruceIrvin 0 Newbie Poster

EDIT: Nevermind, I already found what was wrong; But I can't draw a card properly when I run it still.

import java.util.Scanner;
import java.util.InputMismatchException;

public class CardDealer //Main Class
{

    public static void main(String[] args) {
        Scanner keybd = new Scanner(System.in);
        Deck deck1 = new Deck(true);
        int count = 0;
        int card = 1;
        int deckcount = 0;

        while (card != 0) {
            System.out.println("This is your digital deck of playing cards!");
            System.out.println("A new deck has already been opened and shuffled for you.");
            System.out.print("\n[" + deck1.getSize() + " cards left in deck] How many cards to draw (0 to quit)?");

            try {
                card = keybd.nextInt();
            } catch (InputMismatchException ime) {
                System.out.println("Error: Please enter a number >= 0.");
                System.out.println();
                continue;
            }

            if (card == 0) {
                break;
            }

            while (card >= count) {
                if (deck1.getSize() == 0) {
                    deck1 = new Deck(); //create new Deck.
                }

                deck1.draw();
                count++;

            }

        }
    }
}

//--------------------------------------------------//
class Deck {

    private PlayingCard[] cards;
    private int cardsUsed;
    private boolean shuffled;

    /**
     * Constructs a new deck of 52 standard playing cards with no jokers.
     * If the parameter shuffled == true, the new deck will be shuffled.
     * Otherwise, it will be in sorted order.
     */
    public Deck(boolean shuffled) {
        this.shuffled = shuffled;
        this.shuffled = false;

        this.cards = new PlayingCard[52];
        int count = 0; // How many cards have been created so far.
        
        for (int suit = 1; suit < 5; suit++) {
            for (int value = 1; value <= 13; value++) {
                cards[count] = new PlayingCard(value, suit);
                count++;
            }
        }
        cardsUsed = 0;


        if (shuffled) {
            this.shuffle();
        }
    }

    /**
     * Constructs a deck of 52 shuffled playing cards.
     */
    public Deck() {
        cards = new PlayingCard[52];
        int count = 0; // How many cards have been created so far.
        while (count <= 52) {
            for (int suit = 1; suit <= 5; suit++) {
                for (int value = 1; value <= 13; value++) {
                    cards[count] = new PlayingCard(value, suit);
                    count++;
                }
            }
            cardsUsed = 0;
        }
    }

    /**
     * Removes the top card from this deck and returns it.
     * If this deck is empty, will return null instead.
     */
    public PlayingCard draw() {
        cards.toString();
        cardsUsed++;
        return cards[cardsUsed - 1];
    }

    /**
     * Returns the number of cards currently remaining in this deck.
     */
    public int getSize() {
        return cards.length - cardsUsed;
    }

    /**
     * Shuffles the cards remaining in this deck.
     */
    public void shuffle() {
        for (int i = 51; i > 0; i--) {
            int rand = (int) (Math.random() * (i + 1));
            PlayingCard temp = cards[i];
            cards[i] = cards[rand];
            cards[rand] = temp;
        }
        cardsUsed = 0;
    }
}

//--------------------------------------------------//
class PlayingCard {

    private int value;
    private int suit;
    public static final int CLUBS = 1;
    public static final int HEARTS = 2;
    public static final int SPADES = 3;
    public static final int DIAMONDS = 4;
    public static final int JOKER = 0;
    public static final int ACE = 1;
    public static final int JACK = 11;
    public static final int QUEEN = 12;
    public static final int KING = 13;

    /**
     * Constructs a new card with the given face value and suit.
     *
     * The given value must be between 1 (Ace) and 13 (King).
     * The suit must be one of the CLUBS, SPADES, HEARTS, or DIAMONDS constant values.
     * If either the value or the suit is out of range, both will be set to JOKER.
     */
    public PlayingCard(int value, int suit) {
        this.value = value;
        if (value < 1 || value > 13) {
            this.value = JOKER;
        }

        this.suit = suit;
        if (value < 1 || value > 4) {
            this.suit = JOKER;
        }
    }

    /**
     * Returns the face value of this card: a value between 1 and 13, inclusive,
     * or else JOKER.
     */
    public int getValue() {
        return this.value;
    }

    /**
     * Returns the suit of this card.
     */
    public int getSuit() {
        return this.suit;
    }

    /**
     * Returns a String of this card of the form "4 of Diamonds",
     * where "4" here is the value, and "Diamonds" is the suit.
     * Aces and face card (Jack, Queen, King) names are spelled out.
     *
     * If a card's suit or value is not in the normal range (that is,
     * if either of them contain the value JOKER), the String "Joker"
     * is returned instead.
     */
    @Override
    public String toString() {
        String s = "";

        if (this.value > 0 && this.value < 14) //Between range of 1 - 13
        {
            if (this.value > 1 && this.value < 11) //For value from 2 through 10
            {
                s += this.value; //add value in numerical form
                } else if ((value > 10 && value < 14) || value == 1) //Value is between 11-13, or is 1
            {
                switch (this.value) {
                    case ACE:
                        s += "Ace";
                        break;

                    case JACK:
                        s += "Jack";
                        break;

                    case QUEEN:
                        s += "Queen";
                        break;

                    case KING:
                        s += "King";
                        break;
                }
            }

            s += " of ";

            switch (this.suit) {
                case HEARTS:
                    s += "Hearts";
                    break;
                case DIAMONDS:
                    s += "Diamonds";
                    break;
                case CLUBS:
                    s += "Clubs";
                    break;
                case SPADES:
                    s += "Spades";
                    break;
            }
        } else {
            s += "Joker";
        }
        return s;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.