# Converting ints to Strings Problem

• 12-06-2010, 03:08 AM
gkoef
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:

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.
• 12-06-2010, 06:56 AM
eRaaaa
Use a Map oder an Array / enum ?!
• 12-06-2010, 06:57 AM
jomypgeorge
enum may be useful for your need
• 12-06-2010, 12:32 PM
Vinod Mukundan
Use a hashMap.... or List .... :)
• 12-06-2010, 12:44 PM
gkoef
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
• 12-06-2010, 01:04 PM
gkoef
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.
• 12-06-2010, 01:20 PM
Vinod Mukundan
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
• 12-06-2010, 02:07 PM
JosAH
Quote:

Originally Posted by gkoef
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:

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