Results 1 to 4 of 4
  1. #1
    Kinnikinnick is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default make a variable name from a string?

    Hi all.
    My first post... looking fo help on a school project.

    I'm making a backjack game. I have a Card object that keeps the info about value, suit and card name. I want to make a Deck now with an arrayList containing all the cards I'll need. I don't want to spend so much time writing out the declaring, creating and arrayList adding for EACH card..

    soo I tried to make a for each loop that would make up the names of the variable from 2 more arrayLists containing Strings for suit and card name...
    umm.. I'll post some code and maybe that will explain better.. bha, it's hard for me to explain..

    Thanks for any help you can give!!

    Java Code:
            
            suitNames = new ArrayList<String>();
            suitNames.add("spades");
            suitNames.add("clubs");
            suitNames.add("diamonds");
            suitNames.add("hearts");
            
            cardNames = new ArrayList<String>();
            cardNames.add("Ace");
            cardNames.add("King");
            cardNames.add("Queen");
            cardNames.add("Jack");
            cardNames.add("Ten");
            cardNames.add("Nine");
            cardNames.add("Eight");
            cardNames.add("Seven");
            cardNames.add("Six");
            cardNames.add("Five");
            cardNames.add("Four");
            cardNames.add("Three");
            cardNames.add("Two");
    
    
            int count = 13;
            int value = 11;
    
            for (String suitName : suitNames) {
                for (String cardName : cardNames) {
                    switch (count) {
                        case 13: value = 11; break;
                        case 12:
                        case 11:
                        case 10:
                        case 9: value = 10; break;
                        case 8: value = 9; break;
                        case 7: value = 8; break;
                        case 6: value = 7; break;
                        case 5: value = 6; break;
                        case 4: value = 5; break;
                        case 3: value = 4; break;
                        case 2: value = 3; break;
                        case 1: value = 2; break;
                        default: break;
                    }
                    
                ??(suitName + cardName) = new Card(cardName, suitName, value);??
                }
                count = 13;
            }
    Last edited by JavaBean; 11-12-2007 at 01:12 PM. Reason: Code placed inside [code] tag.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    You are on the right track. I think the switch statement is not needed though. This is how I would do it....

    Java Code:
    Deck d = new Deck();
    for(int i = 0; i < suitNames.size(); i++){
        for(int j = 0; j < cardNames.size(); j++){
            Card c = new card(cardNames.get(j), suitNames.get(i), j + 2);
            d.addCard(c);
        }
    }
    For this to work, you would have to reverse the order in which you add your cardNames to the ArrayList. The card at index zero would be a 'two' and so on.

  3. #3
    Kinnikinnick is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default

    thx for the tip Ninja!

    Problem with it tho... (I forgot to mention this earlier)

    i need to give each card it's own object name so I can use this included method:

    Java Code:
     /**
         * Add a single card to the deck.
         * @param a Card object
         */
        public void addCard(Card newCard)
        {
           deck.add(newCard); // add a card to the deck
        }

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Yeah you're right. I don't know why I did that way. You don't actually need to give it an object name. You can just do something like:

    Java Code:
    addCard(new Card(cardNames.get(j), suitNames.get(i), j + 2));

Similar Threads

  1. how to Parse int to a string variable
    By raj reddy in forum Java Servlet
    Replies: 10
    Last Post: 01-09-2009, 07:41 PM
  2. how to Parse int to a string variable (pls hlp)
    By raj reddy in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-10-2008, 06:32 AM
  3. Help with variable assigment to String
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:43 AM
  4. Replies: 1
    Last Post: 08-03-2007, 10:52 AM
  5. String Variable
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 04:30 AM

Posting Permissions

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