Java code for selecting and comparing cards (without Arraylist)
java
Solution
The easiest thing to do would be to implement a `toString` method in your `Card` class.
public String toString() {
return String.format("%s of %s", stringValue, cardSuit);
}
While you are at it, move the two arrays (`suit` and `value`) out of the constructor, make them `static final`, and rename to all-upper case:
static final String[] SUIT = {"Clubs", "Diamonds", "Hearts", "Spades"};
static final String[] VALUE = {"Ace","Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
Also the selection of the card should not be done like you do:
Card[] playerCard = new Card[r];//create the player's card
creates an array of `r` cards, and assigns it to `playerCard`. This is incorrect: you have already created all your cards in the `Deck`, it's a matter of taking a random one from it:
public Card takeRandomCard() {
int r;
do {
r = (int)(Math.random() * 52);
} while (taken[r]);
taken[r] = true;
return fullDeck[r];
}
Note the little addition here: `taken` is an array of 52 `boolean` objects indicating that the card has been taken from the deck.
Here is your code with my modifications from above working at ideone.
Problem
I see a lot of questions similar to this one but they are answered with code that I haven't learned yet. I am supposed to be able to do this task without an arraylist so that is providing a challenge for me. I need to play a game of war: create a card class, create a deck and then randomly generate the first card, "remove" the card from the deck and produce a 2nd card in order to see which card won, continue this comparison until the deck is exhausted. Here is what I have so far: ``` public class Card { private int cardValue; private String cardSuit; private String stringValue; static final String[] SUIT = {"Clubs", "Diamonds", "Hearts", "Spades"}; static final String[] VALUE = {"Ace","Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; /** *CONSTRUCTOR for Card class *contains the arrays for both suit and values **/ public Card (int cardS, int cardV) { cardSuit = SUIT[cardS]; stringValue = VALUE[cardV]; } /** *Get method for card suit, access the value of cardSuit **/ public String getCardSuit() { return cardSuit; } /** *get method for card's VALUES **/ public String getValue() { return stringValue; } //in order to display string value of cards public String toString() { return String.format("%s of %s", stringValue, cardSuit); } ``` } ``` public class Deck { private final int HIGH_SUIT=3; private final int HIGH_VALUE=12; int i = 0; int cardCount; Card[] fullDeck = new Card[52]; public Deck() { for(int s = 0; s <= HIGH_SUIT; s++) { //for loop to determine the suit for (int v = 0; v <= HIGH_VALUE; v++) { //construct all 52 cards and print them out fullDeck[i] = new Card(s, v); cardCount = (i + 1); i++;//increment the card counter } } } ``` } ``` public class War3 { public static void main(String[] args) { int i=0; Deck[] fullDeck = new Deck[52]; //set up the deck //create a random value for the first card int r = ((int) (Math.random() * 100) % 51);//create random number Card[] playerCard = new Card[r];//create the player's card System.out.println("The card is: " + playerCard.toString()); } ``` As you can see, I didn't get very far with War 3 because I don't know how to display the first card. When run it displays this:The card is: [LCard;@4a5ab2random#1 How do I display the first card in the array? I need help figuring out how to assign the first card and the 2nd card, display them both then compare them. I've got a long way to go so one step at a time.