Jerry Murphy
Jerry Murphy

Reputation: 348

At a complete loss why eclipse is giving this error

I am copying code from Java: How to program 7th edition. This is the code of the 2 classes

public class Card {
    private String face; //face of card
    private String suit; // suit of card

    //2 argument constructor initializes card's face and suit
    public Card(String cardFace, String cardSuit)
    {
        face = cardFace;
        suit = cardSuit;
    }

    // return String representation of Card
    public String toString()
    {
        return face + " of " + suit;
    }
 }

Which is fine but when I start entering the second class I get weird error...

import java.util.Random;

public class DeckOfCards
{
    private Card[] deck;
    private int currentCard;
    private static final int NUMBER_OF_CARDS= 52;
    // random number generator
    private static final Random randomNumbers = new Random();

    // constructor fills decks of cards
    String[] faces = { "Ace", "Duece", "Three", " Four", "Five", "Six",      "Seven",  "Eight", "Nine", " Ten", "Jack", "Queen", "King" }; 
    String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };

}

it works fine until here but when I add the next line I get an error saying the previous line is incorrect.

deck = new Card [ NUMBER_OF_CARDS];

These are the 2 errors that eclipses gives me at the line

String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };

Wacthpoint:DeckOfCards[access and modification] -suits -Syntax error on token ";", { expected after this token. Just to be clear these errors don't appear until I add the line after it.

Upvotes: 0

Views: 43

Answers (2)

Sydul Fahim Pantha
Sydul Fahim Pantha

Reputation: 1

You can change the initialization sequence of the variable deck and NUMBER_OF_CARDS.

public class DeckOfCards
{
        private static final int NUMBER_OF_CARDS= 52;
        private Card[] deck = new Card [ NUMBER_OF_CARDS];

        private int currentCard;

Upvotes: -1

M A
M A

Reputation: 72854

You must place that line in a constructor or method:

public DeckOfCards() {
    deck = new Card[NUMBER_OF_CARDS];
    // fill deck cards here, e.g. deck[0] = new Card(faces[0], suits[0]);
}

Upvotes: 2

Related Questions