Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35
  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Help setting values to cards in an array

    Try debugging your code by adding println() statements to show execution progress and how variable values are changing. For example:
    Add a: System.out.println("var=" + var);
    after every line where a variable is changed by an assignment statement or read into.
    Print out the value returned by getFace() and the values looked at in faces

    I dont see how getCardValue() can return zero. It always adds one to value on line 10
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help setting values to cards in an array

    I'm still stuck at this point... I think I'm putting the wrong argument into the getHandValue() method. I'm not sure what to put in there..

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Help setting values to cards in an array

    A method named getHandValue() should probably get a hand for an argument.
    Where and how is a hand defined? Is there a Hand class? What values would a Hand object contain?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help setting values to cards in an array

    I put print statements in the getCardValue() method and nothing printed.

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Help setting values to cards in an array

    Is the method called from anywhere in the code?
    If it is not called, then none of its code would execute.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    A method named getHandValue() should probably get a hand for an argument.
    Where and how is a hand defined? Is there a Hand class? What values would a Hand object contain?
    This is my main args:

    Java Code:
    public static void main( String args[] )
       {
          DeckOfCards myDeckOfCards = new DeckOfCards();
          myDeckOfCards.shuffle(); // place Cards in random order
          
          Card[] hand = new Card[ 2 ]; // store two cards
    
          // get first two cards
          for ( int i = 0; i < 2; i++ )
          {
             hand[ i ] = myDeckOfCards.dealCard(); // get next card
             System.out.println( hand[ i ] );
          } // end for
          
          myDeckOfCards.getHandValue(hand);

  7. #27
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Help setting values to cards in an array

    That looks like getHandValue shoud be called. Does the code compile without errors? It shouldn't execute if there are compiler errors.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help setting values to cards in an array

    Where would I call the getCardValue() method? Inside the getHandValue() method? Where inside would I put it?

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

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    That looks like getHandValue shoud be called. Does the code compile without errors? It shouldn't execute if there are compiler errors.
    There are no errors right now. The total is just coming out as 0. But I haven't called my getCardValue() method anywhere yet.

  10. #30
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Help setting values to cards in an array

    If you held a hand of cards, how would you manually sum up their values?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    If you held a hand of cards, how would you manually sum up their values?
    Find out their face values, then add them up. I know I call the getcardValue() method first, but do I call it in the beginning of getHandValue() or just in my main args? Also what was the point in creating a card object in getHandValue()? I haven't used it anywhere.

  12. #32
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Help setting values to cards in an array

    You call the getCardValue() method when you want the value of a card.
    what was the point in creating a card object in getHandValue()?
    I have no idea why you'd do that.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help setting values to cards in an array

    Okay, so I tried calling the getCardValue() method on line 24, there isn't any errors, but now the total is always coming out as 12. Why?! :( I can't see anything wrong with the code. Can you see what I'm doing wrong and point it out?

    Java Code:
    public int getCardValue(Card card) {
            int val;
            val = 0;
            for (int i = 0; i > faces.length; i++) {
                if (faces[i].equals(card.getFace())) {
                    val = i;
                    break;
                }
            }
            value = val + 1; //adds 1 since the array starts at 0
            return value;
        }
         
             
        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);
        }

  14. #34
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Help setting values to cards in an array

    Try debugging your code by adding println() statements to show execution progress and how variable values are changing. For example:
    Add a: System.out.println("var=" + var);
    after every line where a variable is changed by an assignment statement or read into.

    I'm done for today.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help setting values to cards in an array

    Okay well thanks so much for your help! It means a lot.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Issue with setting values in an array
    By pyat77 in forum New To Java
    Replies: 4
    Last Post: 11-04-2011, 02:01 AM
  2. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  3. Setting up a String Array
    By AJArmstron@aol.com in forum New To Java
    Replies: 12
    Last Post: 04-16-2010, 12:42 PM
  4. setting and getting array
    By jgonzalez14 in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 06:54 AM
  5. setting format for FLoat values
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:22 PM

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
  •