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

    Question Converting ints to Strings Problem

    Is it possible to convert an int to a String such as an int with the value of 1 to a String with the value of "Ace"?

    Ive tried using the Integer.toString command but it just outputs the integer of 1 converted to the String of "1"

    The coding I have done so far in this class is shown below:

    Java Code:
    public class Card
    {
        	public final static int DIAMONDS = 1;						//Suits
        	public final static int CLUBS    = 2;
        	public final static int HEARTS   = 3;
        	public final static int SPADES   = 4;
    
        	public final static int ACE   = 1;						//Ranks
        	public final static int TWO   = 2;
        	public final static int THREE = 3;
        	public final static int FOUR  = 4;
        	public final static int FIVE  = 5;
        	public final static int SIX   = 6;
        	public final static int SEVEN = 7;
        	public final static int EIGHT = 8;
        	public final static int NINE  = 9;
        	public final static int TEN   = 10;
        	public final static int JACK  = 11;
        	public final static int QUEEN = 12;
        	public final static int KING  = 13;
    
     	public static void main(String[] args)
    	{
    		for(int rank = ACE; rank <= KING; rank++)
    		{
    			for(int suit = DIAMONDS; suit <= SPADES; suit++)
    			{
    				[COLOR="Red"]String A ="Ace";
    				String B ="Diamonds";
    
    				A = Integer.toString(ACE);
    				B = Integer.toString(DIAMONDS);[/COLOR]
    
    
    				System.out.println(A + " of " + B);		//testing
    				
    			}
    		}
    	}
    }
    Can anyone tell me where I am going wrong?

    The program I am trying to create is to have the above class create one object at a time containing a rank and a suit (eg. Ace of Diamonds), which sends the object to another class that makes up a deck of cards. A third class outputs the deck of cards.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Use a Map oder an Array / enum ?!

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    enum may be useful for your need

  4. #4
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Use a hashMap.... or List .... :)
    _______________________________________________
    give me beans .........

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

    Default

    I am new to Java and do not know about enums. I am trying to learn from the Java Tutorials which are on the Oracle Website and learning enums, I am not up to that part yet, and this problem is from one of the exercises. Ive tried putting ranks and suits in different arrays which caused problems (see String Variable Problem thread). So, I started again and made each rank and suit into its own variable. Still having problems.

    Is using the Java Tutorials a good way of learning Java or is there a better way?

    I would go to night classes to learn it but there seems to be nowhere in my area that teaches it

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

    Default

    It seems that the way I am doing this program I am doing it the hard way. Everyone who has posted to my threads have mentioned enum or hashmap which seems to be the easiest way of doing it. I have been trying to work it out for two weekends and a few hours after finishing work most nights and I have hardly got my feet off the ground. So, I am not going to bother with this exercise and carry on with the tutorial. When I learn about enum, I will realise my mistakes.

    Thanks for all the help.

    I know I can come back and get good advice on other programming problems, which no doubt will occur.

  7. #7
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Just going through the API will help u out in this particular issue... Appreciate your enthusiasm for learning....


    Best of Luck..

    warm regards
    Vinod M
    _______________________________________________
    give me beans .........

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

    Default

    Quote Originally Posted by gkoef View Post
    I am new to Java and do not know about enums. I am trying to learn from the Java Tutorials which are on the Oracle Website and learning enums, I am not up to that part yet, and this problem is from one of the exercises. Ive tried putting ranks and suits in different arrays which caused problems (see String Variable Problem thread). So, I started again and made each rank and suit into its own variable. Still having problems.

    Is using the Java Tutorials a good way of learning Java or is there a better way?

    I would go to night classes to learn it but there seems to be nowhere in my area that teaches it
    What you can do with enums you can do without enums (more or less); here goes: suppose each Card (it's a separate class) has an absolute rank in the range 0 ... 51 (there are 52 different playing cards). Note that the rank%13 is a number in the range 0 ... 12 and rank%4 is a number in the range 0 ... 3.
    If you have two String arrays:

    Java Code:
    String[] rank= { "ace", "deuce", "three", ... };
    String colour= { "spades", "hearts", ... };
    The color and rank of a particular card r are rank[r%13] and colour[r%4]. I leave it to you to assemble all this in one proper class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Strings Problem
    By Freakzoyd in forum New To Java
    Replies: 11
    Last Post: 11-30-2010, 02:45 AM
  2. Help with ints
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 03:53 PM
  3. checking for ints in a String
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 06:09 PM
  4. [SOLVED] problem with strings
    By sandeepsai39 in forum New To Java
    Replies: 11
    Last Post: 02-23-2009, 04:59 PM
  5. Problem Comparing Strings (its not what you think)
    By hilather in forum New To Java
    Replies: 7
    Last Post: 11-19-2008, 06:43 PM

Posting Permissions

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