user2057138
user2057138

Reputation: 33

card cannot be resolved to a variable

I'm working on a personal project. It will be a card game that you might for instance compare to pokemon. unfortunately, I'm running into an error and I can't figure out what causes it. I'd appreciate any help!

okay, so I've got the card class (I've left out unnessesary attributes) with a constructor

public class Card 
{
    String name;
    String cardID;
    int strFire;
    int strEarth;

    public Card(String n, String id, int fire, int earth)
    {
        name = n;
        cardID = id;
        strFire = fire;
        strEarth = earth;
    }
}

then I've got the Deck class, which should create the instances of all cards.

public class Deck 
{
    static void createDeck()
    {
        Card hoax06 = new Card("Nirwadas the Traveler", "hoax06", 3, 2);
        System.out.println(hoax06.name); // this works
    }
}

and finally, I've got the Game class which holds main.

public class Game 
{
    public static void main(String[] args) 
    {
        Deck.createDeck();
        System.out.println(hoax06.name); // hoax06 cannot be resolved to a variable
    }
}

I know the answer is probably quite simple but java's accessing system still confuses me. I've also browsed the forum for similar errors but have been unable to apply them to my case. how should I refer to a card from within main?

Upvotes: 2

Views: 192

Answers (4)

Penelope The Duck
Penelope The Duck

Reputation: 656

You could create an enum (perhaps called Cards), listing all your cards, then access them from inside your Game class (or anywhere else) by using Cards.HOAX06.name

For example something like this:

    enum Cards {
        HOAX06("Nirwadas the Traveler", 3, 2),
        HOAX07("Something else", 5, 2) 
        //list all your cards here in the same way
        ;

        String name;
        int strFire;
        int strEarth;

        Cards(String name, int fire, int earth){
           this.name = name;
           this.strFire = fire;
           this.strEarth = earth;
        }
    }

Then to call it from another class:

public static void main(String[] args) {     
     System.out.println(Cards.HOAX06.name);
     System.out.println(Cards.HOAX07.strEarth);
     System.out.println(Cards.HOAX06.strFire);
     }

All Enums are static and final by default you couldn't change the values once they were created, but then I'm guessing that for a deck of cards you probably won't want to..

Upvotes: 0

Arpit
Arpit

Reputation: 12797

try this:

public class Deck 
{
    static Card createDeck()
    {
        Card hoax06 = new Card("Nirwadas the Traveler", "hoax06", 3, 2);
        System.out.println(hoax06.name); // this works
        return hoax06;    //return the object
    }
    }
public class Game 
{
    public static void main(String[] args) 
    {
        Card c=Deck.createDeck();  //get the object
        System.out.println(c.name); // use it here
    }
}

Upvotes: 1

Dennisch
Dennisch

Reputation: 7553

The card instance is created in the Deck class, so the Game class has no direct knowledge of it. Also, as soon as the createDeck() method ends you lose your reference to the card and the instance is gone.

Here's a simple example:

public class Deck 
{
    public Card hoax06;

    static Deck createDeck()
    {
        Deck deck = new Deck();
        deck.hoax06 = new Card("Nirwadas the Traveler", "hoax06", 3, 2);
        return deck;
    }
}

public class Game 
{
    public static void main(String[] args) 
    {
        Deck deck = Deck.createDeck();
        System.out.println(deck.hoax06.name); // this is where the error occurs
    }
}

Upvotes: 2

tckmn
tckmn

Reputation: 59333

You never made hoax06. That is why the error is occurring.

Deck.createDeck();
System.out.println(hoax06.name);

Here are some options:

  1. Make createDeck() return the card
  2. Make a static Card in Deck, so you could use Deck.hoax06.name

Upvotes: 1

Related Questions