Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Anyone have any ideas why I having this error Exception in thread \"main\" java.

ID: 642148 • Letter: A

Question

Anyone have any ideas why I having this error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
   at card.Deck.(Deck.java:27)
   at card.MainDriver.main(MainDriver.java:8)

// cards

public class Cards {
  
   private Suit suit;
   private Value face;
   public Card(Suit aSuit, Value aFace)
{
suit = aSuit;
face = aFace;   
}


//toString method
public String toString()
{
return face + " of " + suit;
}

public boolean winner(Cards c) {
       // TODO: complete this method definition
  
   // winner method return the winner of two cards

   if(face.compareTo(c.face) > 0)
   return true;
   else if(face.compareTo(c.face) == 0 && suit.compareTo(c.suit) >= 0)
   return true;
   else
   return false;
   }

}

//enum suits

public enum Suit {
   Clubs, Diamonds, Hearts, Spades

//enum Value

public enum Value {
   One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace;

//Deck

import java.util.Random;

public class Deck {

   private Random random;
   private final int NUMBER_OF_CARDS = 51;
   private Card[] cards;
   private CardSuit [] suits;
   private CardValue [] faces;
   private int index;
     
   public Deck()
   {
   suits = Suit.values();
   faces = Value.values();
   index = 0;
     
   cards = new Card[NUMBER_OF_CARDS];
     
   for(CardValue f: faces)
   {
       for(CardSuit s : suits)
     
   {
   cards[index] = new Card(s, f); //<- 8
   index++;
   }
   }
     
   random = new Random();
   }

   // draw method returns a random card
   public Card draw()
   {
   int s = random.nextInt(suits.length);
   int f = random.nextInt(faces.length);
   Cards card = new Cards(suits[s], faces[f]);
   return cards;
   }
   }

//MAIN

static final int HANDS = 52;
  
   public static void main(String[] args) {

       Deck Player1 = new Deck(); //<- 27
       Deck player2 = new Deck();
      
   //   int player1Score = 0, player2Score = 0;
       for (int i = 0; i < 5; i++){
          
           Card c1 = Player1.draw();
           Card c2 = Player1.draw();
          
           System.out.println("Card1: " + c1);
           System.out.println("Card2: " + c2);
          
           if(c1.winner(c2))
           System.out.println("Winner: " + c1);
           else
           System.out.println("Winner: " + c2);
           System.out.println();
          
       }
}

}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
   at card.Deck.(Deck.java:27)
   at card.MainDriver.main(MainDriver.java:8)

the error are in the arrow

Explanation / Answer

In each Suit there are 14 values

You initialized cards array for 51 elements

But when you are assigning here i.e,

for(CardValue f: faces)
   {
       for(CardSuit s : suits)
     
   {
   cards[index] = new Card(s, f); //<- 8
   index++;
   }
   }

There 4*14 = 56 assignments but array size is of 51 Therefore you are getting array index out of bounds.

As for every suit there are 14 values so total for 4 suites there are 14*4 values you are setting to cards array.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote