Sign in to follow this  
World69star

Help with sorting a String array based on its index in java

Recommended Posts

I have a simple console based game I am trying to make (in java) and I can't figure out how to sort this array deck[] because it is a string array. I want to be able to sort and then print them in value order(greater cards after lesser cards in same suits) when they are dealt out in my main class. Can I sort the array based on the index number? If so how do you do it? OR do I cast it as a int, how can I do that with this array? Arrays always confuse me! I;d appreciate any help thanks; /code public class Cards { private String[] Deck = new String[52]; private boolean[] UsedCard = new boolean [52]; /** Creates a new instance of Cards */ public Cards() { Deck[0] = "Ace Hearts"; Deck[1] = "2 Hearts"; Deck[2] = "3 Hearts" ; Deck[3] = "4 Hearts"; Deck[4] = "5 Hearts"; Deck[5] = "6 Hearts"; Deck[6] = "7 Hearts"; Deck[7] = "8 Hearts"; Deck[8] = "9 Hearts"; Deck[9]= "10 Hearts"; Deck[10] = "Jack Hearts"; Deck[11] = "Queen Hearts"; Deck[12] = "King Hearts"; Deck[13] = "Ace Diamonds"; Deck[14] = "2 Diamonds"; Deck[15] = "3 Diamonds"; Deck[16] = "4 Diamonds"; Deck[17]= "5 Diamonds"; Deck[18]= "6 Diamonds"; Deck[19]= "7 Diamonds"; Deck[20]="8 Diamonds"; Deck[21]="9 Diamonds"; Deck[22]="10 Diamonds"; Deck[23]="Jack Diamonds"; Deck[24]="Queen Diamonds"; Deck[25]="King Diamonds"; Deck[26]="Ace Spades"; Deck[27]="2 Spades"; Deck[28]="3 Spades"; Deck[29]="4 Spades"; Deck[30]="5 Spades"; Deck[31]="6 Spades"; Deck[32]="7 Spades"; Deck[33]="8 Spades"; Deck[34]="9 Spades"; Deck[35]="10 Spades"; Deck[36]="Jack Spades"; Deck[37]="Queen Spades"; Deck[38]="King Spades"; Deck[39]="Ace Clubs"; Deck[40]="2 Clubs"; Deck[41]="3 Clubs"; Deck[42]="4 Clubs"; Deck[43]="5 Clubs"; Deck[44]="6 Clubs"; Deck[45]="7 Clubs"; Deck[46]="8 Clubs"; Deck[47]="9 Clubs"; Deck[48]="10 Clubs"; Deck[49]="Jack Clubs"; Deck[50]="Queen Clubs"; Deck[51]="King Clubs"; //init used cards to false for(int n=0; n <UsedCard.length;n++) { UsedCard[n]=false; System.out.println("" + UsedCard[n]+n); } } public String getCard() { int cardNumber; Boolean Flag = false; do { //generate a random number >0 <100 cardNumber = (int)(Math.round(Math.random()*100)); if(cardNumber <52) // if > 52 generates a new number if(UsedCard[cardNumber]==true) //test if card is allready used { Flag = false; }else // an unused card was found { UsedCard[cardNumber] = true; Flag= true; } else //random # is greater than 51 outside of the array { Flag= false; } } while(Flag==false); return Deck[cardNumber]; } public void newDeal() { for(int n=0; n<UsedCard.length;n++) { UsedCard[n]=false; } } } /code

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this