I have an array of cards. It looks like this

String cards[][] = {{"Spade", "Heart", "Club", "Diamond"},

how would I get the suit and the "rank" of the card. I have tried

System.out.println (cards[1][5] + " of " + cards [1]);

but that gave me this

6 of [Ljava.lang.String;@1242719c

which is not what I wanted it to print.

How could I solve this issue?

5 Years
Discussion Span
Last Post by Taywin

Because the index cards[1] is an object of String array. If I am not wrong, your current array contains...
cards[0] = ["Spade", "Heart", "Club", "Diamond"]
cards[1] = ["1", "2", ..., "K"]

Try calling cards[2] to see if it throws an exception...

That's because the way you declare it... You shouldn't use 2D array to do the job you are thinking that way... You could use a HashMap or create your own Card class.

Edited by Taywin: n/a



System.out.println (cards[1][5] + " of " + cards [0][1]);

This will print "6 of Heart".
Your first index [0] are your suit(S), and first index [1] are your rank(S).

Edited by hfx642: n/a


Even though this thread is marked as solved, I still want to say something. Please do not design a 2D array variable with different length. You may see issues having that design in the future. If you want to do it that way, either make it as a 1D array or use Hash type. I didn't give you the way to call the value because I do not want to convince you that it is OK to do so... Please keep in mind.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.