Acid Forums

Full Version: Urgent java help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I need java help again
I have this card class:

class Card {

   private final char suit;
   private final int face;

   initialises with suit ('S'=spade, 'H'=heart, 'D'=diamonds, 'C'=clubs)
   and face (1=ace, 2, ... 10, 11=knight, 12=queen and 13=king).

   public Card(char suit, int face) {
       this.suit = suit;
       this.face = face;

   public String toString() {
       return String.format("%s%s", suit, face);

   public char getSuit() {
       return suit;

   public int getFace() {
       return face;

I'm supposed to make a Deck class that creates a deck of cards. This is my attempt, and I'm not sure if I've done it right. I've tried creating the deck as well as printing the deck, but it doesn't seem to work.

class Deck {
   public static void main(String[] args) {

       Card[] cards;

       public Deck(int n){
  = new Card[n];

       public Deck() {
  = new Card[52];
           int index = 0;
           for (int suit = 0; suit <= 3; suit++) {
               for (int rank = 1; rank <= 13; rank++) {
         [index] = new Card(rank, suit);

       Deck deck = new Deck();

       public void print () {
           for (int i = 0; i <; i++) {

I get an illegal start of expression error at line 6 ( public Deck (int n) { )
Okay I got it to work, this is my new Deck class

public class Deck1 {
   public static void main(String[] args) {
       String[] SUITS = {
               "Clubs", "Diamonds", "Hearts", "Spades"

       String[] RANKS = {
               "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
               "Jack", "Queen", "King"

       // initialize deck
       int n = SUITS.length * RANKS.length;
       String[] deck = new String[n];
       for (int i = 0; i < RANKS.length; i++) {
           for (int j = 0; j < SUITS.length; j++) {
               deck[SUITS.length*i + j] = RANKS + " of " + SUITS[j];

       // print deck
       for (int i = 0; i < n; i++) {

next task is to create an Assign method in the Deck class. This method should pick random n cards from the deck and return these in a collection. n is a number between 1 and 52 which is sent in as a parameter to the assign method. And honestly I have no idea how to do this.
Which part do you not know how to do?

If it's how to make a random number, it's the Math.random() function.

If it's the Assign function,
- A Collection in Java can be a List or other things, an ArrayList should suffice here. So the function can be like
static LinkedList<String> assign(int n)

The tricky part would be to make sure that the same card in the deck don't get included twice in the collection you return, if that's required by the task.

If your program doesn't need to be efficient, however, there are many brute-force-ish way to do this, such as adding everything in the deck to a list, which you then use Collections.shuffle with and take the first n cards, or just keep picking a random one by making a random index number, picking the card of that index number and removing the card, and repeat until you get n cards or you have no more cards in the deck.
It's linking the random number method with the assign function. I've shuffled the deck, so the deck is in an array, but I'm not sure how I get the program to pick out the n first cards.
Got it to work, but yeah, I think I need to make sure the same card is not printed twice. Here's my code, I think this should be correct.

// defining range
int min = 1;
int max = 51;
int range = max - min + 1;

// print n cards in deck
for (int i = 0; i < range; i++) {
   int n = (int)(Math.random() * range) + min;
Yes, that code should work to take a random card without preventing drawing the same card twice.

I already said how to do that above, and it's easier to do that by using list rather than only array, since after removing the cards in the middle you need to manually fill in that slot by moving the other card(s).

Though for this problem, you can always just move the last card in the array to that empty slot, and adjust the random range so that the range doesn't include empty cards, which would now be at the end of the array.