Question: Just looking for some help with my code. I've got most of it finished, but I'm getting an ArrayIndexOutOfBoundsException at Deck.next(Deck.java:53) when the index should
Just looking for some help with my code. I've got most of it finished, but I'm getting an ArrayIndexOutOfBoundsException at Deck.next(Deck.java:53) when the index should definitely not be out of bounds. Would anyone more experienced be able to offer some insight and see if they can get this code working? I've posted the instructions and my three classes below:

CARD
public class Card { private String character; public Card(String deckSuit, String deckValue) { character = (deckSuit + ", " + deckValue); } public Card(String deckCharacter) { character = deckCharacter; }
public String toString() { return "[" + character + "]"; } }
import java.util.Random;
public class Deck
{
Card[] deck;
private int dealt = 0;
public Deck(String[] suit, String[] value)
{
int temp = (suit.length*value.length);
deck= new Card[temp];
int counter = 0;
for (int i = 0; i { for (int j = 0; j { deck[counter] = new Card(suit[i], value[j]); counter ++; } } } DECK public Deck(String[] character) { int temp = (character.length); deck= new Card[temp]; int counter = 0; for (int i = 0; i { deck[counter] = new Card(character[i]); } } public Deck() { int temp = (52); deck= new Card[temp]; String[] arr1 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; String[] arr2 = {"\u2665", "\u2666", "\u2660", "\u2663"}; int counter = 0; for (int i = 0; i { for (int j = 0; j { deck[counter] = new Card(arr2[i], arr1[j]); counter ++; } } } public Card next() { dealt++; return deck[dealt]; } public boolean hasNext() { if (next() != null) { return true; }else{ return false; } } public int remaining() { return (deck.length-dealt); } public int size() { return deck.length; } public void shuffle() { dealt=-1; Random rand = new Random(); for (int i=0; i int randomPosition = rand.nextInt(deck.length); Card temp = deck[i]; deck[i] = deck[randomPosition]; deck[randomPosition] = temp; } } } TEST class Test { public static void main(String[] args) { String[] array1 = {"A","B","C"}; String[] array2 = {"1","2"}; Deck cards = new Deck(array1, array2); Card curCard = cards.next(); String cardsString = curCard.toString(); while (cards.hasNext()) { curCard = cards.next(); cardsString = cardsString + " " + curCard.toString(); } System.out.println(cardsString.equals("[A,1] [A,2] [B,1] [B,2] [C,1] [C,2]")); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
