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.

I told you before and warned you about creating 2D array with different length... Because of the odd way of creating 2D array, you cannot simply use the for-loop that way to retrieve the value from the array you created... And that's why I didn't show you how to access the odd designed 2D array...

// look at your loop
for (int i = 0; i < 4; i++) {
  for (int z = 0; z < 13; z++) {
    deck.add (cards[i][z]);
  }
}
/*
 What is containing inside cards are...
 card[0] = {"Spade", ...}
 card[1] = {"A", "2", ...}

 Now tell me what happen when i=0 and z=4??? What is the value of cards[0][4]???
 Of course, it is out of bound...
 That's where your error occurs.
*/

I'm blaming the person who told you how to access it without pointing out possible problems in the future.

** Your original post is here.

I remade it so that I wrote out all the cards. Now it works

I have another null pointer exception now. I have separated the class into multiple classes. Here is the deck class.

import java.util.Vector;


public class Deck {

	Player player;
	AI comp1;
	AI comp2;
	AI comp3;
	
	String cards[][] = { 
			{"AS","2S","3S","4S","5S","6S","7S","8S","9S","1S","JS","QS","KS"},
			{"AC","2C","3C","4C","5C","6C","7C","8C","9C","1C","JC","QC","KC"},
			{"AD","2D","3D","4D","5D","6D","7D","8D","9D","1D","JD","QD","KD"},
			{"AH","2H","3H","4H","5H","6H","7H","8H","9H","1H","JH","QH","KH"}
			};
	Vector<String> deck = new Vector<String>();
	
	
	Deck (){
		for (int i = 0; i < 4; i++){
			for (int z = 0; z < 13; z++){
				deck.add (cards[i][z]);
			}
		}
		
	}


	public void passInPlayers(Player p, AI c1, AI c2, AI c3) {
		// TODO Auto-generated method stub
		player = p;
		comp1 = c1;
		comp1 = c2;
		comp1 = c3;
		deal();
	}


	private void deal() {
		// TODO Auto-generated method stub
		System.out.println ("Size of deck vector: " + deck);
		for (int p = 0; p < 4; p++){
			System.out.println ("Giving to player: " + p);
			for (int i = 0; i < 13; i++){
				int cardNum = (int) Math.floor(Math.random() * deck.size());
				if (p == 0){
					player.cards[i] = "" + deck.elementAt(cardNum);
				}
				if (p == 1){
					comp1.cards[i] = "" + deck.elementAt(cardNum);
				}
				if (p == 2){
					comp2.cards[i] = "" + deck.elementAt(cardNum);
				}
				if (p == 3){
					comp3.cards[i] = "" + deck.elementAt(cardNum);
				}
				deck.remove(cardNum);
			}
		}
		printOutCards ();
	}


	private void printOutCards() {
		// TODO Auto-generated method stub
		System.out.println ("Player 1:");
		for (int i = 0; i < 13; i++){
			System.out.print (player.cards[i] + ", ");
		}
		System.out.println ();
		System.out.println ("Player 2:");
		for (int i = 0; i < 13; i++){
			System.out.print (comp1.cards[i] + ", ");
		}
		System.out.println ();
		System.out.println ("Player 3:");
		for (int i = 0; i < 13; i++){
			System.out.print (comp2.cards[i] + ", ");
		}
		System.out.println ();
		System.out.println ("Player 4:");
		for (int i = 0; i < 13; i++){
			System.out.print (comp3.cards[i] + ", ");
		}
	}
	
	
	
	
}

While I am dealing I get a null pointer exception and I do not know why. Thanks for any help.

From the code so far, I do not see any error that could cause NPE from your "deck" variable. However, I am not sure about your "Player" or "AI" object. What exactly the error said?

Please "always" include the whole error you see when you compile/run. It is not nice to throw in your code and said "not working" or "got an error of such and such" without giving the error statements you got from doing so. It is difficult for everyone to look through all of your code for an error or two...

I'm sorry. I forgot to post the error. Here it is:

Setting up players
going to deck
Size of deck vector: [AS, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, 1S, JS, QS, KS, AC, 2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 1C, JC, QC, KC, AD, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, 1D, JD, QD, KD, AH, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, 1H, JH, QH, KH]
Giving to player: 0
Giving to player: 1
Giving to player: 2
Exception in thread "main" java.lang.NullPointerException
	at Deck.deal(Deck.java:54)
	at Deck.passInPlayers(Deck.java:36)
	at Paints.setUpPlayers(Paints.java:18)
	at Hearts.<init>(Hearts.java:18)
	at Hearts.main(Hearts.java:26)

and here is the player class. So far the player class and the AI class are the same.

public class Player {

	Paints paints;
	String [] cards = new String[13];
	int cardNumber = 0;
	
	public void passIn (Paints p){
		paints = p;
	}
	
	public void getCards (String currentCard){
		cards[cardNumber] = currentCard;
		cardNumber ++;
	}
	
}

The error caused by line 34 (and will be in line 35 as well). You have already set "comp1 = c1;" and then do it again in "comp1 = c2;" and "comp1 = c3;". Then later on, you try to access comp2 which is not set to any thing.

PS: Warning for your "Player" class for method getCards(). You are not validating value of "cardNumber" before use which could cause an exception in the future.

Thanks for the help. That fixed the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.