I am making hearts, and I cannot seem to add all the cards to the deck. here is my code
import java.lang.reflect.Array;
import java.util.Vector;
public class Hearts {
String cards[][] = {{"Spade ", "Heart ", "Club ", "Diamond "},
{"A","2","3","4","5","6","7","8","9","10","J","Q","K"}};
Vector <String> deck = new Vector<String> ();
String player1[] = new String[13];
String player2[] = new String[13];
String player3[] = new String[13];
String player4[] = new String[13];
Hearts (){
for (int i = 0; i < 4; i++){
for (int z = 0; z < 13; z++){
deck.add (cards[i][z]);
}
}
dealCards ();
System.out.println ("Player 1: \n" + player1);
System.out.println ("Player 2: \n" + player2);
System.out.println ("Player 3: \n" + player3);
System.out.println ("Player 4: \n" + player4);
}
private void dealCards() {
// TODO Auto-generated method stub
for (int p = 0; p < 4; p++){
for (int i = 0; i < 13; i++){
int cardNum = (int) Math.floor(Math.random() * deck.size());
if (p == 0){
player1[i] = "" + deck.elementAt(cardNum);
}
if (p == 1){
player2[i] = "" + deck.elementAt(cardNum);
}
if (p == 2){
player3[i] = "" + deck.elementAt(cardNum);
}
if (p == 3){
player4[i] = "" + deck.elementAt(cardNum);
}
deck.remove(cardNum);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Hearts h = new Hearts ();
}
}
It gives me an array index out of bounds exception for my vector.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Hearts.<init>(Hearts.java:21)
at Hearts.main(Hearts.java:58)
I don't know what the problem is. Thanks for any help.