Ok

I would like to use Deck in a third class.

Deck myDeck = new Deck();


Then in paint I try

g.drawString("" + myDeck[1].suit, 50,50);

It says 'array required, but Deck found'


If I try

Deck[] myDeck = new Deck();


I get 'incompatable types'

I hate asking for help BUT I need the correct way to use(instantiate) a Deck object.

Thanks in advance

public class Card
{
public int rank;
public String suit;
}


public class Deck
	{
		private int random = 0;
		private Card[] deck = new Card[52];
		final String[] suit = {"hearts", "diamonds", "spades", "clubs"};
		
		public Deck()
		{
			int cardNumber = 0;
			for(int suitNo = 0; suitNo < 4; suitNo++)
			{
				for(int rank = 1;rank < 14; rank++)
				{
					deck[cardNumber] = new Card();
					deck[cardNumber].suit = suit[suitNo];
					deck[cardNumber].rank = rank;
					cardNumber++;
				}
			}
		}
		
		
	}

Recommended Answers

All 4 Replies

To create an array of Deck objects, you need to initialize the array with the amount of elements:

Deck[] d = new Deck[3];

Then,

d[0] = new Deck();


Like that....Let me know if you need a little more information.

My next qusetion would be WHY?


--------------------Configuration: Ch13_4 - j2sdk1.4.2_08 <Default> - <Default>--------------------
E:\CIS1500\Junk\Ch13\Ch13_4\src\Ch13_4.java:14: ']' expected
deck[0] = new Deck();
^

E:\CIS1500\Junk\Ch13\Ch13_4\src\Ch13_4.java:14: <identifier> expected
deck[0] = new Deck();
^

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Ch13_4 extends Applet implements ActionListener 
{
	
//	private Button deal, twist, stick;
//	private TextField plyr, comp;
//	private Label lblPlyr, lblComp;
//	private int pScore, cScore, button;

	Deck[] deck = new Deck[3];
	deck[0] = new Deck();  //this is line 14
	
	
	public void init() 
	{
//		deal = new Button("Play Again");
//		add(deal);
//		deal.addActionListener(this);
//		twist = new Button("Twist");
//		add(twist);
//		twist.addActionListener(this);
//		stick = new Button("Stick");
//		add(stick);
//		stick.addActionListener(this);
//		lblPlyr = new Label("Player score:");
//		add(lblPlyr);
//		plyr = new TextField(10);


//		add(plyr);
//		lblComp = new Label("CPU score");
//		add(lblComp);
//		comp = new TextField(10);
//		add(comp);
		
		
		
	}
	public void actionPerformed(ActionEvent event)
	{
	
	}

	
	
	
	
	
	public void paint(Graphics g) 
	{
		
		//g.drawString("" + deck[1].suit, 50,50);
		
	}
	
	
	


}

Your line 14 is illegal there, it can only exist inside a method or an (static) initialiser block.

Sorry about that vex, I thought it would work. I'm looking for a solution right now, but I really thought that would work.

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.