Results 1 to 7 of 7

Thread: Please help

  1. #1
    Kar
    Kar is offline Member
    Join Date
    Dec 2015
    Posts
    2
    Rep Power
    0

    Default Please help

    How do I incorporate a string of numbers:private static final String[] FACE_VALUES = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
    into my code? I am trying to create a blackjack game and cannot make this work. For the J, Q, K, and A I have already assigned 10s and 11 or 1 for A depending on the situation. Thanks!

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Please help

    1) A title "Please help" doesn't cover your post. Please use descriptive post titles. (Trust me, everybody needs help here at this forum)
    2) What do you mean when you say: "incorporate a string of numbers"? What does that actually mean?
    3) When I see "doesn't work", it means I will need my crystal ball to figure it out, and that is expensive. Alternatively, you might want to elaborate on what doesn't work. Does it compile? Does it run? What did you put in and what did you get out? Was there an error? Was there any output at all? Was this output expected?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Please help

    Quote Originally Posted by Kar View Post
    How do I incorporate a string of numbers:private static final String[] FACE_VALUES = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
    into my code? I am trying to create a blackjack game and cannot make this work. For the J, Q, K, and A I have already assigned 10s and 11 or 1 for A depending on the situation. Thanks!
    A 'string of numbers' doesn't make any sense; a string can contain string representations of numbers (separated by what?) but it can't contain numbers; you are showing us an array of strings and most of the elements are string representations of a number (except for J, Q, K and A); as shown that array doesn't contain any syntax errors, so maybe that's what you want: an array of strings?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    7

    Default Re: Please help

    The OP probably wants to associate a face value with each playing card. HashMap or HashTable should be your friend here.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: Please help

    And here's the kicker: an ace can either have "value" 1 or 11; that's sometimes even up to the player to decide :)

    Me personally, I'd just create a dumb method getCardValue().

    Java Code:
    public int getCardValue(String card){
      // TODO: think about it
    }
    And based on the card input as a String, return an appropriate number. Don't make it any more difficult than it has to be. That still leaves the problem of the ace of course, but that may be a problem for a later stage of development.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Kar
    Kar is offline Member
    Join Date
    Dec 2015
    Posts
    2
    Rep Power
    0

    Default Re: Please help

    Sorry for my vagueness, my first time writing one of these. I have put the J, Q, K, and A into a method
    public int getValue() {
    if (numberValue == 0) {
    for (int i = 2; i <= 10; i++) {
    if (faceValue.equals("" + i)) {
    numberValue = i;
    break;
    }
    }
    if (faceValue.equals("J") || faceValue.equals("Q") || faceValue.equals("K"))
    numberValue = 10;
    else
    numberValue = 11;
    }
    return numberValue;
    }
    public String getFaceValue(){
    return faceValue;
    }

    and I use card.getValue to retrieve the information from the above code in a player class.
    ie. System.out.println("You have the " + cards.get(0).getValue() + " of " + cards.get(0).getSuit() + " and the " + cards.get(1).getValue() + " of " + cards.get(1).getSuit() + ".");
    Yet when i try to use the array of strings I cannot simply use deck.FACE_VALUES
    What do I need to use?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Please help

    What are you trying to do? Given a card's value represented by a String, eg "2" or "K" are you trying to get an int value eg 2 or 10 for that card?

    One simple way would be to use a switch statement. It allows the use of Strings in the case statements.

    If you were to create a class for a card, it could hold both the String and the int value for the card.
    If you don't understand my response, don't ignore it, ask a question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •