-1

I am currently working on a java project in which I must use the cards of a normal deck.

I would like to make a String Cards to hold the value of the different cards. I started with this.

String Card = "12345678910JQK"

I realized it was not the right way because the 10 is actually a 1 and then a 0 I am wondering if it is possible to make a String with number and letters.

  • 1
    You probably want to read about java enums ( https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html ). Strings are a really bad choice to represent things that can be enumerated in such ways. – GhostCat Jun 19 '18 at 13:26
  • Don't forget jokers... – Brian Jul 18 '18 at 19:51

5 Answers5

4

You could use:

  • An enum
  • a String array
  • a Collection
  • A String but with a separator, e.g. 1,2,3,4...,9,10,J,... and use .split(',') to work with it.
Andreas Hartmann
  • 1,825
  • 4
  • 23
  • 36
3

In this case, since it's a finite number of things you want to represent, why not use an Enum with all the values for the cards?

Here you have some examples on enumaration, and it's explained with cards, it might be a good read:

https://howtoprogramwithjava.com/enums/

  • 1
    That is much more of an comment than an answer. And when you decide to answer such a low quality question, you really want to make up on quality. Like including links or code examples. Just ending the whole thing with a question mark is a clear indication that your answer is far from being an answer. – GhostCat Jun 19 '18 at 13:27
2

I doubt by using String you can solve this issue. If i was at your place i would have used Object array to solve this problem like

Object  obj[] = {1,2,3,4,5,6,"J","Q","K"};
 System.out.println(obj[8]);
 System.out.println(obj[1]);

lets wait for some answer to come on this .

Karan
  • 443
  • 5
  • 14
1

you should use for this split(,) but before you should declare you string like this, String card="1,2,3,4,5,6,7,8,9,10,J,Q,K"

or you should use enum or String array for this problem.

Yohan Malshika
  • 716
  • 1
  • 11
  • 20
0

String array is the easiest way I think.

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

then you can access each card as card[0], cards[1].