Member Avatar for sammoto

So I'm trying to write a program to play the card game Go Fish, and I can't get the following code to compile:

import java.util.Random;

public class CardPile {
    public static final int DECKSIZE = 52;
    private Card[] cards;
    private int numCards;
    public CardPile() {
        cards = new Card[DECKSIZE];
        numCards = 0;
    }
    public void addToBottom(Card c) {
        if (numCards == 52) {
            System.out.println("The deck is full, you cannot add a card.");
        } else {
            for(int i = 0;i < 52;i++) {
                int count = 0;
                while (count <= 1) {
                    if (cards[i] == null) {
                        cards[i] = c;
                        numCards++;
                    }
                    count++;
                }
            }
        }
    }
    public Card removeCard(Card c) {
        for(int i = 0;i < 52;i++) {
            if (cards[i].getValue() == c.getValue() && cards[i].getSuit() == c.getSuit()) {
                cards[i] = null;
                for(int shift = 1; shift < (cards.length() - i); shift++) {
                    cards[(i+shift)] = cards[(i+(shift-1))];
                }
                return c;
            } else {
                return null;
            }
        }
    }
    public Card removeTop() {
        cards[0] = null;
        int i = 0;
        for(int shift = 1; shift < cards.length(); shift++) {
                    cards[shift] = cards[(shift+1)];
        }
    }
    public int removeAll(int value) {
        int cardsRemoved = 0;
        for(int i = 0;i < 52;i++) {
            if (value == cards[i].getValue()) {
                cards[i] = null;
                cardsRemoved++;
            }
        }
        return cardsRemoved;
    }
    public int searchValue(int value) {
        int count = 0;
        for(int i = 0;i < 52;i++) {
            if (cards[i].getValue() == value) {
                count++;
            }
        }
        return count;
    }
    public int getNumberCards() {
        return numCards;
    }
    public String toString() {
        for(int i = 0;i < 52;i++) {
            System.out.print(cards[i].toString() + ", ");
        }
    }
    private static Random r = new Random(1);
    public void shuffle() {
        int i = r.nextInt(numCards-1);
        int j = r.nextInt(numCards-1);
        for(int repeat = 0;repeat <=100000;repeat++) {
            Card temp = new Card(0, 0);
            temp = cards[i];
            cards[i] = cards[j];
            cards[j] = cards[i];
        }
    }
    public static Card[] makeFullDeck() {
        for (int i = 0;i < 52;i++) {
            for (int suitCount = 0;suitCount < 4; suitCount++) {
                for (int valueCount = 1;valueCount < 14; valueCount++) {
                    cards[i] = new Card(suitCount, valueCount);
                    //Card c = new Card(suitCount, valueCount);
                    //cards.addToBottom( c);
                }
            }
        }
        CardPile.shuffle();
        return cards;
    }
}

This code is the type definition for objects like the deck and the players' hands. Here's the error I get when I try to compile it:

CardPile.java:31: cannot find symbol
symbol  : method length()
location: class Card[]
                for(int shift = 1; shift < (cards.length() - i); shift++) {
                                                 ^
CardPile.java:43: cannot find symbol
symbol  : method length()
location: class Card[]
        for(int shift = 1; shift < cards.length(); shift++) {
                                        ^
CardPile.java:89: non-static variable cards cannot be referenced from a static context
                    cards[i] = new Card(suitCount, valueCount);
                    ^
CardPile.java:95: non-static method shuffle() cannot be referenced from a static context
        CardPile.shuffle();
                ^
CardPile.java:96: non-static variable cards cannot be referenced from a static context
        return cards;
               ^
5 errors

I just don't get the whole non/static thing. I thought I did, but apparently not... Also, if "cards" is a Card array (I've definied the Card type in another class), why can't I get the length of it like I would say an int array? Sorry it's so long, I just thought I might as well post all of it.

Thanks!

First of all you dont have Card Class, thats why it is giving cant find symbol error.
Second you cant call non-static objects from static functions because static has more life then non static thats why compiler stops you from doing this.

Length of an array is .length - not a method call so no ().

Non static members have a different value for every instance of the class, so its not valid to reference them without knowing which instance you are referring to. In a static method there is no particular instance, so you can't refer to a nonstatic member without qualifying it with an instance.

Member Avatar for sammoto

So how do I "qualify it with an instance"? I hear what you're both saying, but I don't understand it. You're just repeating exactly what my computer told me and what my textbook tells me. I need to see some example of how this problem is fixed in order to get a better understanding

And thanks for the .length tip, just a simple mix-up on my part.

In an instance method there's always a current instance, so you can refer directly to its instance variables - eg someVar = 0;
In a static method (or another class) you need an instance, eg
MyClass someInstance = new MyClass();
then you can refer to that instance's members by using the instance, eg
someInstance.someVar = 0;

Member Avatar for sammoto

K, so the method that's throwing those last three errors is supposed to make a full deck and shuffle it. When I write the main game logic (in the main method of a new class), I'll be declaring a new variable of type CardPile, the deck. So if "deck" is going to be the instance of CardPile on which I want to call the makeFullDeck method, then I want to write Card deck.cards[i] = new Card(suitCount, valueCount)?

Member Avatar for sammoto

I'm trying to "make" a full deck of cards. So for each suit, my loops have to cycle though each value to create a total of 52 cards in the Card array "cards". It's really not clear to me what I need to tell my computer to do in order to accomplish this... Same with the shuffle() method, that's a non-static method and I'm trying to call it from a static method, but I have to use that method to shuffle the deck

The question has been marked as solved, so I don't know whether you're still expecting answers...

When you find yourself calling static methods from non-static methods or vice versa, it means you haven't thought about the problem enough. You actually answered your own question after a fashion, if you take the time to consider it:

Same with the shuffle() method, that's a non-static method and I'm trying to call it from a static method, but I have to use that method to shuffle the deck

To shuffle what? A deck, which is an instance of the CardPile class. The CardPile class itself is not -- can not be -- a deck; it's more of a blueprint that describes how CardPile objects (instances) behave. You can't call CardPile.shuffle() because the CardPile class doesn't contain any cards or anything to shuffle.

What this boils down to is that within the makeFullDeck method, you must actually instantiate an object of class CardPile, with the new keyword and the constructor you wrote, and call the shuffle() method on that instance.

Making any more sense?

Member Avatar for sammoto

Yes, thank you!

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.