Results 1 to 5 of 5
  1. #1
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Question Help adding array values.

    I am writing a Java program that plays blackjack. I am having a lot of trouble with my method that adds up the face values of the cards. The faces of the cards are in an array:

    Java Code:
        String faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
            "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    I have made a method called getHandValue() that is supposed to add up the sum of the cards. (Three of Kings + Jack of Spades = 13). My getHandValue() method looks like this:

    Java Code:
        public void getHandValue(Card hand[]) {
            int HandTotal;
            boolean ace;
     
            HandTotal = 0;
            ace = false;
     
            for (int i = 0; i < hand.length; i++) {
                int cardValue;
                cardValue = getCardValue(hand[i]); 
                
                if (cardValue > 10) {
                    cardValue = 10; //sets Jack, Queen and King to 10
                }
                if (cardValue == 1) {
                    ace = true;
                }
                            
                HandTotal = HandTotal + cardValue;
            }
            if (ace == true && HandTotal + 10 <= 21) {
                HandTotal = HandTotal + 10;
            }
            System.out.printf("Hand total: %d\n", HandTotal);
        }
    This method, when given the correct location in the array of the face that is on the card, will find the face value of the card. I'm pretty sure this method works correctly, what I'm having trouble with is the getCardValue() method. It keeps returning a value of 1, so the getHandValue() method keeps returning a total of 12, no matter what cards are put through it. This is my getCardValue() method:

    Java Code:
    public int getCardValue(Card hand) {
            int val;
            val = 0;
            for (int i = 0; i > faces.length; i++) {
                if (faces[i].equals(hand.getFace())) {
                    val = i;
                    break;
                }
            }
            value = val + 1; //adds 1 since the array starts at 0
            return value;
        }
    I know that it always returns a value of 1, but I'm not sure why. The val variable should set to the position of the face array that the card matches to (Ace = 0, Jack = 10), but it doesn't. Can someone see why and show me where I am wrong? And how to go about fixing it? Thanks in advance!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Help adding array values.

    Stick some printlns in there to see what values are getting into your various methods, and what values it is dealing with at various stages.
    Essentialy, debug it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help adding array values.

    Quote Originally Posted by Kirstielol View Post
    Java Code:
            for (int i = 0; i > faces.length; i++) {
    Ahem ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help adding array values.

    Quote Originally Posted by Tolls View Post
    Stick some printlns in there to see what values are getting into your various methods, and what values it is dealing with at various stages.
    Essentialy, debug it.
    I did, which is how I narrowed it down to my getCardValue() method not working, specifically the for statement.

  5. #5
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help adding array values.

    Quote Originally Posted by JosAH View Post
    Ahem ...

    kind regards,

    Jos
    Oh wow I found it -.-" Such a small mistake. Thanks for your help, I have it working now!

Similar Threads

  1. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  2. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  3. Adding up all values generated from a for-loop
    By BariMutation in forum New To Java
    Replies: 4
    Last Post: 10-10-2010, 05:31 PM
  4. Adding Values of 2 Arrays
    By core2duo5252 in forum Java 2D
    Replies: 7
    Last Post: 09-05-2009, 03:50 AM
  5. BlackJack help please,, adding hand values
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 04:26 AM

Tags for this Thread

Posting Permissions

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