• 03-20-2012, 07:22 PM
Kirstielol
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:

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:

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:

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!
• 03-20-2012, 07:28 PM
Tolls
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.
• 03-20-2012, 07:33 PM
JosAH
Quote:

Originally Posted by Kirstielol
Code:

`        for (int i = 0; i > faces.length; i++) {`

Ahem ...

kind regards,

Jos
• 03-20-2012, 07:34 PM
Kirstielol
Quote:

Originally Posted by Tolls
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.
• 03-20-2012, 07:42 PM
Kirstielol