Results 1 to 5 of 5
Thread: String variable problem
- 12-05-2010, 01:24 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 24
- Rep Power
- 0
String variable problem
First I will show you the coding (with errors ) below:
Java Code:import java.util.*; public class Deck { private Card[][] playingcards; public Deck(String rank, String suit) { for(rank = Card.CardRank[0]; rank [COLOR="Red"]<=[/COLOR] Card.CardRank[11]; rank[COLOR="Red"]++[/COLOR]) { for(suit = Card.CardSuit[0]; suit [COLOR="Red"]<=[/COLOR] Card.CardSuit[3]; suit[COLOR="Red"]++[/COLOR]) { //playingcards = new Card[rank][suit]; } } } }
The errors above are all to do with that CardSuit and CardRank are String arrays in another class called Card.
Is there any way in which I can change <= and ++ for String variables? or is it possible to convert a string of variables in an array to int variables without typing out each variable to int?Last edited by Fubarable; 12-05-2010 at 01:32 PM. Reason: Mod Edit: Code tags added
- 12-05-2010, 01:29 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
Playing cards have become a canonical example for the use of enums. Read this article.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
I've added code tags to your first post so that the code is readable. Please read the link in my signature below to learn how to do this yourself.
I think that your problem comes from trying to use <= and such on Strings, but can't tell for sure without the actual error messages -- so please post these. If I'm right, then a solution is to loop on the int indexes and then use them inside the loop, something like this:
Java Code:for(int rank = 0; rank < 13; rank++) { for(int suit = 0; suit < 4; suit++) { playingcards[rank][suit] = new Card(Card.CardRank[rank], Card.CardSuit[suit]); } }
But having said this, Jos is right. This problem is taylor-made to be solved by enums.
- 12-05-2010, 02:08 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 24
- Rep Power
- 0
I remember that in the exercise it states to keep the coding for the Enum tutorial lesson which is going to discussed later in the tutorial. But isn't it strange to give an exercise for something that has not been taught yet?
-
Similar Threads
-
Convert variable name to string.
By MHardeman25 in forum New To JavaReplies: 9Last Post: 08-17-2010, 09:18 PM -
Object name by string variable?
By zerkz in forum New To JavaReplies: 4Last Post: 10-14-2009, 07:16 AM -
make a variable name from a string?
By Kinnikinnick in forum New To JavaReplies: 3Last Post: 11-13-2007, 03:54 PM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM -
String Variable
By Eric in forum Advanced JavaReplies: 1Last Post: 06-06-2007, 04:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks