Results 1 to 5 of 5
  1. #1
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Question 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Playing cards have become a canonical example for the use of enums. Read this article.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  4. #4
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default

    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?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by gkoef View Post
    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?
    No, there's nothing strange about it. Just because it is most cleanly solved as an enum, doesn't mean that it can't be solved well by other techniques. If you haven't studied enums yet, don't use them, but also don't try to use Strings as the indices of your for loop.

Similar Threads

  1. Convert variable name to string.
    By MHardeman25 in forum New To Java
    Replies: 9
    Last Post: 08-17-2010, 09:18 PM
  2. Object name by string variable?
    By zerkz in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 07:16 AM
  3. make a variable name from a string?
    By Kinnikinnick in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:54 PM
  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
  •