I have an array of cards. It looks like this

String cards[][] = {{"Spade", "Heart", "Club", "Diamond"},
	{"1","2","3","4","5","6","7","8","9","10","J","Q","K"}};

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?

Recommended Answers

All 5 Replies

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.

Member Avatar for hfx642

Try

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).

are you heard about Enum, that would be something for this job,

Thanks. That solved the problem.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.