-5

Let me introduce you to the components of my problem. A Array called cards that has been created in the main method. A class called Card that has 2 class variables, suit and number. A ArrayList called ph (stands for playerHand) in a public class called Hand and finally a static public variable called counter. So now let me explain my dilemma.

In the main method the Array cards contains 52 card objects each with their own data. In the hand class I have a method called draw().

Here is some of the code inside the hand class.

ArrayList<Card> ph = new ArrayList<Card>();
public static int cc = 0;



    void draw() {
ph.add(new Card());

ph.get(cc).num = cards[cc].num;
ph.get(cc).suit = cards[cc].suit;

cc = cc + 1;
}

However I have the following error on the 2 lines before cc = cc + 1. "cards cannot be resolved to a variable"

How do I transfer the data between the Array and the ArrayList?

EDIT: This is different from the post this is marked as a duplicate of because here the array is in a different class so that solution wouldn't work.

Foobar
  • 7,458
  • 16
  • 81
  • 161
  • You should include the code where the Array is declared. – Eran Aug 19 '14 at 21:53
  • You wrote, that cards array has been declared in main method, but you try to use it in draw method. Try declaring it as a class field or put this card array as param into the draw method. – Michał Schielmann Aug 19 '14 at 21:53
  • What is `cc` ? You should provide more code – Volune Aug 19 '14 at 21:54
  • Please post a more **complete** example. Is is very hard from the code snippet you provided to analyse your issue. Seems like you should past cards as arguments of your draw method: 'void draw(Card[] cards)'. – ratiaris Aug 19 '14 at 21:55
  • You might want to think about using `Arrays.asList()` instead of manually coping every card. – azurefrog Aug 19 '14 at 21:56
  • The method name `draw()` is confusing. Is this supposed to "draw a card from a deck of cards", or "draw a card on the screen"? It actually affects what the code you've shown us (probably) ought to do. – Stephen C Aug 19 '14 at 22:06
  • Sorry cc is actually count – Foobar Aug 20 '14 at 01:06

1 Answers1

4

However I have the following error on the 2 lines before cc = cc + 1. "cards cannot be resolved to a variable"

That means that you haven't declared cards in a way / place that allows draw() to see the declaration.

You say:

In the main method the Array cards contains 52 card objects each with their own data.

It sounds like you have declared cards as a local variable within the main method. Local variables are only in scope for statements in the current method body.

The cards array either needs to be declared as a field of the enclosing class, or it needs to be passed as a parameter to draw. I can't say which would be better without seeing your complete code.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216