2

I'm trying to create a deck of cards for my homework. Code is posted below. I need to create
four sets of cards (the four suits) and am create a multidimensional array. When I print the results instead of trying to pass the array, I can see that the data in the array is as expected. However, when I try to pass the array card, I get an error cannot find symbol. I've got this modeled after texbook and Java tutorial examples, and I need some help figuring out what I'm missing. I've over-documented to give an idea of how I'm thinking this SHOULD work...please let me know where I've gone horribly wrong in my understanding.

import java.util.*;
import java.lang.*;
//
public class CardGame
{
    public static int[][] main(String[] args)
    {
        int[][] startDeck = deckOfCards();  /* cast new deck as int[][], calling method deckOfCards
        System.out.println(" /// from array: " + Arrays.deepToString(startDeck));
    }

    public static int[][] deckOfCards()   /* method to return a multi-dimensional array */
    {
        int rank;
        int suit; 
        for(rank=1;rank<14;rank++)    /* cards 1 - 13 .... */
        {
            for(suit=1;suit<5;suit++)  /* suits 1 - 4 .... */
            {
                int[][] card = new int[][]    /* define a new card...  */
                {
                    {rank,suit}      /* with rank/suit from for... loops */
                };
                System.out.println(" /// from array: " + Arrays.deepToString(card));
             }
         }
         return card;  /*  Error: cannot find symbol 
    }
}
1
  • You are defining card[][] inside your nested loop, therefore its only visible in its (outer for loop's) scope. Commented Mar 19, 2012 at 23:25

5 Answers 5

4

card only exists within the for loop: variables are only valid within the block ({..}) within which they are declared.

Note rules for the main() method (from section 12.1.4 Invoke Test.main of JLS 3.0):

The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.

Sign up to request clarification or add additional context in comments.

Comments

2

Ahh... Scope is your issue... Look where card is declared relative to where it is returned from. Remember when you declare a variable within a loop or sub function its scope is limited to that location on the stack.. If this doesn't help message me back.

3 Comments

I get the idea of scope, and looking at my code, see why card is limited to just that {} block. The question now becomes whether I move the int card[][] line elsewhere, or if I put int card[][]; before the main method (e.g., global variable) and just create card = new deckOfCards(); where it is? Am I on the right track?
Right track completely depends on what you want to happen. If you instantiate the whole array outside the for loop, you will never accidentally return null. If you dont then you could potentially return null when the loop never gets entered. (This wont happen in your situation I don't believe) Altho if you instantiate the card array where it is now, every time you iterate to a new array slot you are overwriting your old object and loosing that information. best answer declare the whole thing outside the four loop and also instantiate it.
I'll post my code after another 8 hours, but I ended up declaring card as a global...outside the main method, and it works like a dream! Thanks for your help!
1

The reference card is limited to the scope of your inner for loop. A variable only exists inside of the inner most set of braces {} that enclose it.

You want to make one array, outside the loop, and fill it up in the loop. Not make a totally new array for each pass of the loop.

Comments

1

Your card was defined in the for loop. Try with it defined at the top, otherwise it gets redefined every loop, and your function will return null.

import java.util.*;
import java.lang.*;
//
public class CardGame
{
    public static int[][] main(String[] args)
    {
        int[][] startDeck = deckOfCards();  /* cast new deck as int[][], calling method deckOfCards
        System.out.println(" /// from array: " + Arrays.deepToString(startDeck));
    }

    public static int[][] deckOfCards()   /* method to return a multi-dimensional array */
    {
        int rank;
        int suit; 
        int[][] card = new int[][]    /* define a new card...  */
        for(rank=1;rank<14;rank++)    /* cards 1 - 13 .... */
        {
            for(suit=1;suit<5;suit++)  /* suits 1 - 4 .... */
            {

                {
                    {rank,suit}      /* with rank/suit from for... loops */
                };
                System.out.println(" /// from array: " + Arrays.deepToString(card));
             }
         }
         return card;  /*  Error: cannot find symbol 
    }
}

Comments

0

Thanks to everyone who pointed out scope as my issue...that was definitely it. Here's my revised code, which I'm happy to report works fantastically.

import java.util.*;
import java.lang.*;
//
public class CardGame 
{
    static int card[][];   /* card is now a global variable...and static so it can */
                           /* interact with static methods.  */
//
    public static void main(String[] args)
    {
        int card[][];
        int[][] startDeck = deckOfCards();
        System.out.println(" /// from array: " + Arrays.deepToString(startDeck));
    }


    public static int[][] deckOfCards()
    {
        int rank;
        int suit;
        for(rank=1;rank<14;rank++)
        {
            for(suit=1;suit<5;suit++)
            {
                card = new int[][]
                {
                    {rank,suit}
                };
                System.out.println(" /// from array: " + Arrays.deepToString(card));
             }
         }
         return card;
    }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.