Question

I've been fighting with this for the past few days, and am hoping that someone can offer some guidance. First time working with arrays in Java, and only about 2 months into learning Java. Be gentle. :)

In my mind, I'm creating a parallel array, where cardInDeck[][] is being populated with cardName (e.g., Ace, 10, Jack) and suitName (e.g., Hearts, Spades, etc.). While my output from the method and what I ~think~ I'm writing to the array are as expected, I'm getting some anomalous results when printing out the contents of the array.

I'm trying to narrow down if those unexpected results are because I'm structuring the array improperly when I declare it, if I'm trying to create an array that's abhorrent to god and nature (or at least Java) or if the array IS correct, but I'm not understanding how to print the contents.

Any help you can offer would be appreciated. Thanks in advance!

Code and results appear below.

import java.util.*;
import java.lang.*;
//
public class DeckOfCards
{
   public static void main(String[] args)
   {
      int rank;
      int suit;
//
   for(rank=1;rank<14;rank++)
   {
       String tempCardName = Integer.toString(rank);
       String cardName;
       if(rank==1)
       cardName="Ace";
       else
       if(rank==13)
       cardName="King";
       else
       if(rank==12)
       cardName="Queen";
       else
       if(rank==11)
       cardName="Jack";
       else
       cardName=tempCardName;
 //
       for(suit=1;suit<5;suit++)
       {
       String suitName;
       switch(suit)
       {case 1:
   suitName = "Spades";
   break;
   case 2:
   suitName = "Hearts";
   break;
   case 3:
   suitName = "Clubs";
   break;
         case 4:
   suitName = "Diamonds";
   break;
   default:
   suitName = "Error";}
       String tempCardInDeck=(cardName + " of " + suitName);
       System.out.print("method output: " tempCardInDeck);
 //*
       String[][] cardInDeck = new String[][]
          {
             {cardName,suitName}
          };
       System.out.print(" /// to array: " + cardName + " " + suitName);
       System.out.println(" /// from array: " + Arrays.toString( cardInDeck ) );
     }
     }
   }
 }

RESULTS:

method output: Ace of Spades /// to array: Ace Spades /// from array: [[Ljava.lang.String;@9ffb18]
method output: Ace of Hearts /// to array: Ace Hearts /// from array: [[Ljava.lang.String;@11de914]
method output: Ace of Clubs /// to array: Ace Clubs /// from array: [[Ljava.lang.String;@b1406b]
method output: Ace of Diamonds /// to array: Ace Diamonds /// from array: [[Ljava.lang.String;@fc5408]
method output: 2 of Spades /// to array: 2 Spades /// from array: [[Ljava.lang.String;@1f102c1]
method output: 2 of Hearts /// to array: 2 Hearts /// from array: [[Ljava.lang.String;@1ec0130]
method output: 2 of Clubs /// to array: 2 Clubs /// from array: [[Ljava.lang.String;@1420fea]
method output: 2 of Diamonds /// to array: 2 Diamonds /// from array: [[Ljava.lang.String;@230be4]
method output: 3 of Spades /// to array: 3 Spades /// from array: [[Ljava.lang.String;@e1e567]
method output: 3 of Hearts /// to array: 3 Hearts /// from array: [[Ljava.lang.String;@9bfee2]
method output: 3 of Clubs /// to array: 3 Clubs /// from array: [[Ljava.lang.String;@17aaeec]
method output: 3 of Diamonds /// to array: 3 Diamonds /// from array: [[Ljava.lang.String;@1721a26]
method output: 4 of Spades /// to array: 4 Spades /// from array: [[Ljava.lang.String;@12db7c]
method output: 4 of Hearts /// to array: 4 Hearts /// from array: [[Ljava.lang.String;@7c28c]
method output: 4 of Clubs /// to array: 4 Clubs /// from array: [[Ljava.lang.String;@17588d5]
method output: 4 of Diamonds /// to array: 4 Diamonds /// from array: [[Ljava.lang.String;@16a7c99]
method output: 5 of Spades /// to array: 5 Spades /// from array: [[Ljava.lang.String;@1a5d08]
method output: 5 of Hearts /// to array: 5 Hearts /// from array: [[Ljava.lang.String;@d1c9b5]
method output: 5 of Clubs /// to array: 5 Clubs /// from array: [[Ljava.lang.String;@111c3f0]
method output: 5 of Diamonds /// to array: 5 Diamonds /// from array: [[Ljava.lang.String;@156f14c]
method output: 6 of Spades /// to array: 6 Spades /// from array: [[Ljava.lang.String;@fbd1fc]
method output: 6 of Hearts /// to array: 6 Hearts /// from array: [[Ljava.lang.String;@973678]
method output: 6 of Clubs /// to array: 6 Clubs /// from array: [[Ljava.lang.String;@1791620]
method output: 6 of Diamonds /// to array: 6 Diamonds /// from array: [[Ljava.lang.String;@9ba632]
method output: 7 of Spades /// to array: 7 Spades /// from array: [[Ljava.lang.String;@bc5245]
method output: 7 of Hearts /// to array: 7 Hearts /// from array: [[Ljava.lang.String;@1bd523d]
method output: 7 of Clubs /// to array: 7 Clubs /// from array: [[Ljava.lang.String;@6250d2]
method output: 7 of Diamonds /// to array: 7 Diamonds /// from array: [[Ljava.lang.String;@a8198c]
method output: 8 of Spades /// to array: 8 Spades /// from array: [[Ljava.lang.String;@25753d]
method output: 8 of Hearts /// to array: 8 Hearts /// from array: [[Ljava.lang.String;@1341183]
method output: 8 of Clubs /// to array: 8 Clubs /// from array: [[Ljava.lang.String;@169cccc]
method output: 8 of Diamonds /// to array: 8 Diamonds /// from array: [[Ljava.lang.String;@10469e8]
method output: 9 of Spades /// to array: 9 Spades /// from array: [[Ljava.lang.String;@c4fedd]
method output: 9 of Hearts /// to array: 9 Hearts /// from array: [[Ljava.lang.String;@138847d]
method output: 9 of Clubs /// to array: 9 Clubs /// from array: [[Ljava.lang.String;@1826ac5]
method output: 9 of Diamonds /// to array: 9 Diamonds /// from array: [[Ljava.lang.String;@12fb063]
method output: 10 of Spades /// to array: 10 Spades /// from array: [[Ljava.lang.String;@1e55d39]
method output: 10 of Hearts /// to array: 10 Hearts /// from array: [[Ljava.lang.String;@14b525c]
method output: 10 of Clubs /// to array: 10 Clubs /// from array: [[Ljava.lang.String;@c4c05]
method output: 10 of Diamonds /// to array: 10 Diamonds /// from array: [[Ljava.lang.String;@1530551]
method output: Jack of Spades /// to array: Jack Spades /// from array: [[Ljava.lang.String;@18235a1]
method output: Jack of Hearts /// to array: Jack Hearts /// from array: [[Ljava.lang.String;@18ee2ee]
method output: Jack of Clubs /// to array: Jack Clubs /// from array: [[Ljava.lang.String;@1d48043]
method output: Jack of Diamonds /// to array: Jack Diamonds /// from array: [[Ljava.lang.String;@30cd64]
method output: Queen of Spades /// to array: Queen Spades /// from array: [[Ljava.lang.String;@1fc9fee]
method output: Queen of Hearts /// to array: Queen Hearts /// from array: [[Ljava.lang.String;@67f797]
method output: Queen of Clubs /// to array: Queen Clubs /// from array: [[Ljava.lang.String;@1b01949]
method output: Queen of Diamonds /// to array: Queen Diamonds /// from array: [[Ljava.lang.String;@4c2849]
method output: King of Spades /// to array: King Spades /// from array: [[Ljava.lang.String;@1e8f2a0]
method output: King of Hearts /// to array: King Hearts /// from array: [[Ljava.lang.String;@90f19c]
method output: King of Clubs /// to array: King Clubs /// from array: [[Ljava.lang.String;@1e67280]
method output: King of Diamonds /// to array: King Diamonds /// from array: [[Ljava.lang.String;@675039]
Was it helpful?

Solution

What you're seeing in the output is the default implementation of toString. From the documentation for Arrays:

Returns a string representation of the contents of the specified array. If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.

Because it's the default version from Object, what you'll get is...

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

As suggested in another answer, Arrays.deepToString should be what you're after. That's actually a new one to me, so I'll be trying it out for myself.

OTHER TIPS

If you want to print the content of the array, you should use Arrays.deepToString(cardInDeck) instead of Arrays.toString(cardInDeck).

Another way to implement a deck of cards is using enums, which allows the compiler to enforce type checking for you, a very good practice. See Oracle's Java Tutorials for examples of cards, deck, and display at these links.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top