PDA

View Full Version : [SOLVED] [Java] Going over code/logic for blackjack



7nQecEL
October 29th, 2013, 11:04 PM
Hi,

I'm working on an assignment for a blackjack game and I need some help figuring out how to fix an Exception error: "java.lang.ArrayIndexOutOfBoundsException: 52" which has something to do with the shuffle(). I think I implemented it correctly, but maybe there is something I'm not seeing?

Hopefully someone can help me out?
Note 1: For the shuffle() in Deck class, you can replace it with collections if you want. Part of the assignment was to use my own random generator (based on an example in class)

7nQecEL
October 30th, 2013, 04:30 AM
I think I've fixed one of the error that I was having. But I'm still getting the "java.lang.ArrayIndexOutOfBoundsException: 52" error...

Bachstelze
October 30th, 2013, 04:47 AM
What does RandomGenerator.generateRandomNum() do?

7nQecEL
October 30th, 2013, 09:02 AM
it's suppose to generate a random number to use when shuffling the deck and getting random cards. Part of the assignment was to call a method from a jar file. I made that from an example shown in class. You think that might be the problem? It's almost the exact same thing as what was shown in class

ofnuts
October 30th, 2013, 09:26 AM
I'm ready to bet that the way you call it your random generator generates numbers in the range 1..52(*) while your array indices are in the range 0..51.

(*) 52, that, not so surprisingly, appears in the error message...

7nQecEL
October 30th, 2013, 05:52 PM
Gah that was it. Haha can't believe I missed that

Thank you!

ICanHasNick
November 1st, 2013, 02:00 PM
If you can use the Collection Class and it's shuffle() method [note 1], what is the random generator good for?

I'm just asking because the whole task of simulating a (black jack) card deck is already perfectly solved
in the LinkedList Class which implements the Queue interface. You just take cards from the beginning of
the queue and put them to it's end after usage. When you arrive at the first card again you can use
Collection.shuffle(cardDeck)