Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Card shuffle program

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Card shuffle program

    Hello I'm getting an error in my code here
    what I'm trying to do is output a shuffle of 5 cards e.g 4 of diamonds in red

    package javacards;
     
    import java.util.Random;
     
    public class Deck {
    	private Card[] cards;
    	int i;
     
    	Deck()
    	{
    		i=5;
    		cards = new Card[52];
    		int x=0;
     
    		for (int a=0; a<=12; a++)
    		{
    			for (int b=0; b<=3; b++)
    			{
    				for (int c=0; c<=1; c++)	<------- it appears this is the problem but I don't know why
    /*
    This is the error I get 
     
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52
    	at javacards.Deck.<init>(Deck.java:21)
    	at javacards.TestPoker.main(TestPoker.java:7)
     
    */		
    				 {			  
    				cards[x] = new Card(a,b,c); 
    				   x++;
    				 }
    			}
    		}
    	}
     
    	public Card drawFromDeck()
    	{
    		Random generator = new Random();
    		int index=0;
     
    		do {
    			index = generator.nextInt( 52 );
    		} while (cards[index] == null);
     
    		i--;
    		Card temp = cards[index];
    		cards[index]= null;
    		return temp;
    	}
     
    	public int getTotalCards()
    	{
    		return i;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Card shuffle program

    java.lang.ArrayIndexOutOfBoundsException: 52
    The index for the array is going past the end of the array.
    Remember indexes for arrays range from 0 to the array length-1.
    The max index for a 52 element array is 51.

    Print out the value of x to see what its value is getting to.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Card shuffle program

    My problem is I'm not sure where to fix this

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Card shuffle program

    First work out the max values for each of the loop control values. Make it so that the three loops will only cause the inner loop to execute 52 times. The posted code loops 13 x 4 x 2 times.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Card shuffle program

    Ok I got it working when I replace 52 with 104

    My output is

    7 of diamonds in black
    4 of diamonds in red
    Jack of spades in black
    9 of hearts in black
    Jack of hearts in red

    and changes everytime I compile it

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Card shuffle program

    Is that the correct results? Is the code doing what you want now?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Card shuffle program

    Thanks a lot Norm!

    --- Update ---

    Yes it work you're a legend!!!

Similar Threads

  1. What's wrong with my code? Card Program.
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2013, 07:36 PM
  2. Credit Card String Program
    By hotshotennis in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 6th, 2012, 04:17 PM
  3. [SOLVED] Need help with Pick A Card Program Please
    By gcjava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2012, 04:46 AM
  4. HashMap Shuffle Card
    By d'fitz in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2011, 07:00 AM
  5. [SOLVED] I am trying to shuffle up an array
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: July 18th, 2009, 03:31 PM