Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2007, 03:08 PM
Member
 
Join Date: Nov 2007
Posts: 2
Kinnikinnick is on a distinguished road
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!!

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 03:12 PM. Reason: Code placed inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-12-2007, 06:49 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
You are on the right track. I think the switch statement is not needed though. This is how I would do it....

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-13-2007, 08:32 AM
Member
 
Join Date: Nov 2007
Posts: 2
Kinnikinnick is on a distinguished road
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:

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 }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-13-2007, 05:54 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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:

Code:
addCard(new Card(cardNames.get(j), suitNames.get(i), j + 2));
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Parse int to a string variable raj reddy Java Servlet 7 11-11-2008 03:12 PM
how to Parse int to a string variable (pls hlp) raj reddy Threads and Synchronization 5 06-10-2008 08:32 AM
Help with variable assigment to String silvia New To Java 1 08-07-2007 07:43 AM
I can't seem to pass the value of a string variable into a string array mathias Java Applets 1 08-03-2007 12:52 PM
String Variable Eric Advanced Java 1 06-06-2007 06:30 AM


All times are GMT +3. The time now is 10:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org