Reputation: 71
So in this scenario I have an array with multiple cards within a deck. These cards in each index are ranked S1R1, S1R2, S1R3, S2R1, S2R2, S2R3. I need to be able to extract both numbers from each index position and multiply them together and eventually add them up.
For example,
position 0 would be 1 * 1 = 1
position 1 would be 1 * 2 = 2
position 2 would be 1 * 3 = 3
etc. etc.
The code for this example changes based on user input so it's tough to give you the code to work with.
public void createDeckofCards() {
SizeOfDeck = NumberOfRanks * NumberOfSuits;
Cards newCard = new Cards();
newCard.setCards ( NumberOfRanks, NumberOfSuits );
newDeck = new String [ SizeOfDeck ];
int counter = 0;
for (int whatSuit = 1; whatSuit <= NumberOfSuits; whatSuit++) {
for (int rank = 1; rank <= NumberOfRanks; rank++) {
newDeck[counter++] = newCard.createCard(rank, whatSuit);
}
}
}
This is what calls upon method createCard() which creates each card based on how many cards the user inputs. So newDeck[] contains all these cards that I need to break into numbers and summarize eventually. (Making a histogram of 100,000 hands of cards)
Thank you for your time, and I appreciate any input
Upvotes: 0
Views: 99
Reputation: 3894
You can use regex
^S([\\d]+)R([\\d]+)$
Code Snippet:
String s ="S3R4";
Pattern pattern = Pattern.compile("^S([\\d]+)R([\\d]+)$");
Matcher matcher = pattern.matcher(s);
if(matcher.find()){
System.out.println((Integer.parseInt(matcher.group(1))*Integer.parseInt(matcher.group(2))));
}
Output:
12
Upvotes: 1
Reputation: 16518
Make your card an object (Card
perhaps) with properties of suit and rank.
Give it methods to return:
toString
is a logical choice): return "S" + suit + "R" + rank;
getValue()
): return suit * rank;
With that, you can create an array of Card
instead of an array of String
.
If you need the ability to convert the symbolic representations ("S1R7", "S3R9") to the physical object, create a Map<String, Card>
. You can populate it with map.put(card.toString(), card);
.
To look up a card you just need to Card card = map.get("S1R2");
Then the value is available as card.getValue()
Upvotes: 1