0

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

4
  • 1
    I'm a little confused what you are asking. Could you sum up your problem in a single sentence? Commented Jun 22, 2018 at 1:12
  • I'm not asking for someone to solve my homework... I am trying to figure out how to extract numbers from an array. I could see it being possible if I find a way to take these numbers and put them into a new array, and then multiply positions 0 & 1, 2 & 3, etc. Commented Jun 22, 2018 at 1:14
  • But wouldn't that not be possible because it's currently a string with letters S & R in each index? I figure the index needs to be broken apart first to pick out the numbers individually @RobbyCornelissen Commented Jun 22, 2018 at 1:20
  • @Twissted You can use regex ^S([\\d]+)R([\\d]+)$ Please see my answer below: stackoverflow.com/a/50979561/1262248 Commented Jun 22, 2018 at 1:51

2 Answers 2

1

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Make your card an object (Card perhaps) with properties of suit and rank. Give it methods to return:

  • a symbolic form (toString is a logical choice): return "S" + suit + "R" + rank;
  • a value form (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()

2 Comments

Interesting, that makes a lot of sense actually. Thank you for your input
I believe this is the way it is meant to be done but with all the rest of my code being completed as is and I cannot seem to conceptualize creating a Card object for each card made then returning this object to be saved within an array I am most likely going to use the regex. I will continue messing around with this for tn though. Ty again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.