Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2010, 03:10 AM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
Corey is on a distinguished road
Default ArrayIndexOutOfBoundsException
Hey, I'm new to java and I'm working on a simple program to try and run a simulated card game. I'm trying to construct a deck of 52 cards that will be in random order. Cards have been defined and list is an array of 52 cards, in order. For some reason I am getting an error on my randomizer method, even though I've double checked to ensure the indices are valid yet i'm still getting the ArrayIndexOutOfBoundsException thrown at me. Perhaps i've over looked something, here is the code that contains the error.

private Card[] randomizer()
{
int count = 1;
int num;
int index = 1;
boolean located;
Card[] randomDeck = new Card[52];
int nums[] = new int[52];
for (int x = 1; x <= 52; x++)
nums[x - 1] = x;
while (count <= 52)
{
index = 1;
located = false;
num = (int)(Math.random()*52+1);
while (index < 52 || located == false)
{
if (nums[index - 1] == num) //Error: ArrayIndexOutOfBoundsException
{
randomDeck[count - 1] = list[num -1];
nums[index - 1] = 0;
located = true;
count++;
}
else
index++;
}
}
return randomDeck;
}

Thank you for any help
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-01-2010, 03:40 AM
Senior Member
 
Join Date: Nov 2009
Posts: 229
Rep Power: 1
collin389 is on a distinguished road
Default
Here is an easy shuffle algorithm from Wikipedia called the Fisher-Yates shuffle.
Code:
public static void shuffle(int[] array) 
{
    Random rng = new Random();       // java.util.Random.
    // n is the number of items left to shuffle
    for (int n = array.length; n > 1; n--) 
    {
        // Pick a random element to move to the end
        int k = rng.nextInt(n);  // 0 <= k <= n - 1.
        // Simple swap of variables
        int tmp = array[k];
        array[k] = array[n - 1];
        array[n - 1] = tmp;
    }
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-01-2010, 04:26 AM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
Corey is on a distinguished road
Default
interesting, i'll try that instead, seems like a lot less headache
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-01-2010, 04:45 AM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
Corey is on a distinguished road
Default
Hmm, that seems to have fixed the IndexOutOfBounds exception, but now I get the NullPointerException when I try to use that method in one of my constructors. Im pasting below: the constructor, the revised randomizer method and, the static list array, in case that helps.

public Deck()
{
Card[] random = randomizer();
for (int i = 1; i <= 26; i++)
deck.add(random[i-1]); //ERROR HERE
}

private Card[] randomizer()
{
Random rng = new Random(); // java.util.Random.
// n is the number of items left to shuffle
Card[] randomDeck = list;
for (int i = 1; i <= 52; i++)
randomDeck[i - 1] = list[i - 1];
for (int n = randomDeck.length; n > 1; n--)
{
// Pick a random element to move to the end
int k = rng.nextInt(n); // 0 <= k <= n - 1.
// Simple swap of variables
Card tmp = randomDeck[k];
randomDeck[k] = randomDeck[n - 1];
randomDeck[n - 1] = tmp;
}
return randomDeck;
}

private static Card[] list; // list is a static array, so im not sure if that is affecting the constructor through the randomizer method. If so, i need the data in list, so should I simply copy the data manually, and am i creating an alias and really shuffling list (which i would like to remain in order)?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-01-2010, 05:49 AM
Senior Member
 
Join Date: Nov 2009
Posts: 229
Rep Power: 1
collin389 is on a distinguished road
Default
First of all, in your loop:
Code:
for (int i = 1; i <= 52; i++)
randomDeck[i - 1] = list[i - 1];
you can instead say:
Code:
for (int i = 0; i < 52; i++)
randomDeck[i] = list[i];
and it will do the same thing

Second, why dont you just add cards 0-51 to the deck, and then use Collections.Shuffle(); It works a lot better.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-02-2010, 01:25 AM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
Corey is on a distinguished road
Default
Thanks, and i think i've fixed the problem
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
array, error, out of bounds

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Suprising ArrayIndexOutOfBoundsException!!!!!! nmvictor New To Java 5 11-18-2009 10:21 PM
ArrayIndexOutOfBoundsException flaskvacuum New To Java 6 07-14-2009 05:36 PM
[SOLVED] ArrayIndexOutOfBoundsException thelinuxguy Advanced Java 1 05-07-2009 10:34 PM
ArrayIndexOutofBoundsException help filly444 New To Java 9 09-03-2008 05:16 PM
ArrayIndexOutOfBoundsException daredavil82 New To Java 2 12-14-2007 09:29 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 05:02 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org