Member Avatar for glebovg

Create a class called CardDeck with a main method contents exactly as shown here:

{
String[] deck=new String[52];
createDeck(deck);
for(int i=0; i<52; i++)
System.out.println(deck[i]);
}

You should create a method called createDeck which populates the array you created in the main method. You must use four loops, each creates the 13 cards for one of the four suits. Within the loop the card names should be automatically generated and inserted in the array. (e.g., “Ace of Spades”, “2 of Spades”…”King of Hearts”, etc.) You must use loops to generate the card names, you must not create 52 literal Strings.

public class CardDeck
{
	public static void main(String[] args)
	{
	String[] deck=new String[52];
	createDeck(deck);
	for(int i=0; i<52; i++)
	System.out.println(deck[i]);
	}
	public static void createDeck(String[] myDeck)
	{
	String[] num={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
	int count=0;
	for(int x=0; x<13; x++)
	myDeck[x]=num[x]+" of Diamonds";
	count++;
	for(int x=13; x<26; x++)
	myDeck[count]=num[count]+" of Spades";
	count++;
	for(int x=26; x<39; x++)
	myDeck[count]=num[count]+" of Hearts";
	count++;
	for(int x=39; x<52; x++)
	myDeck[count]=num[count]+" of Clubs";
	} // end createDeck
} // end class CardDeck

What is wrong with my code?

What is wrong with my code?

Can you explain what the problem is? Does it compile?
Does it execute?
What are the results of the execution?
Please post the output and explain what is wrong with it.

Member Avatar for glebovg

I get this:

Ace of Diamonds
2 of Spades
3 of Hearts
4 of Clubs
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null

Which is not the desired result. It is suppose to print the whole deck.

What is the output supposed to look like?
The output changes suits on each of the first four lines. Is that what you want?

Now look at the second for loop.
What are the values of the variables used in the loop?
Look especially at the index for the array where you are storing the Strings? When does it's value change?

You need to take a piece of paper and write down what the indexes and value should be for each String you are storing into the myDeck array. Then work on the code so it uses those indexes and values as it stores Strings into the array.

Member Avatar for glebovg

It is supposed to be:

Ace of Diamonds
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Spades
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
King of Spades

etc.

I am not sure what exactly I need to change.

You need to take a piece of paper and write down what the indexes and value should be for each String you are storing into the myDeck array. Then work on the code so it uses those indexes and values as it stores Strings into the array.

One problem I see with your code is that it is not properly formatted.
Your indentations are not correct. Every nesting level in the logic should be indented 3-4 spaces. Your code has most of the statements in line vertically making it hard to see the nesting levels of the logic.
If you edit your code and do proper indentation you will see your problem.

Member Avatar for glebovg

I really do not understand what is wrong with my code. Is there something wrong with line 17?

Did you format the code as I asked?
That will show you one of your problems.

Member Avatar for glebovg

Can you help with my code and not formatting? Point out a mistake in code.

If you format your code you will see one of your problems. Proper formatting will show you one of your problems.

For the other problem you need to take a piece of paper and write down what the indexes and values should be for each String you are storing into the myDeck array. Then work on the code so it uses those indexes and values as it stores Strings into the array.

Member Avatar for glebovg

I prefer my formatting. You are not actually helping me. To me it looks correct. I am not good at Java. I need help with my code.

Your formatting is hiding your problem.
If you want to get better at Java I suggest that you use the recommended formatting for code. Formatting is important. It allows anyone to easily read the code and understand the nesting levels of the statements and to see what statements are inside of a loop and what statements are not in the loop.

I've said it 4 times now and I guess if you don't want to follow my suggestions there is nothing more I can do.

Good luck.

Member Avatar for glebovg

Can anyone actually help?

Member Avatar for ztini
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class CardDeck {
	
	// list to hold deck
	private List<String> deck = new ArrayList<String>();
	
	// instantiate deck
	public CardDeck() {
		createDeck();
	}

	public void createDeck() { 
		
		// create 2 arrays to hold Strings to be concatenated together as a card
		String[] values = new String[] { 
				"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"
		};
		
		String[] types = new String[] {
				"Diamonds", "Spades", "Hearts", "Clubs"
		};
		
		// outer loop over the types, inner loop over the values to create cards
		// add cards to your deck list
		for (String type : types) {
			for (String value : values)
				deck.add(MessageFormat.format("{0} of {1}", value, type));
				// Use MessageFormat.format or StringBuffer for string concatenation.  Never use: String + String
		}
		
	}
	
	// always override Object.toString() if you're just outputting the values of an object to the console.
	@Override
	public String toString() {
		StringBuffer stringBuffer = new StringBuffer();
		for(String card : deck) {
			stringBuffer.append(card);
			stringBuffer.append("\r\n");
		}
		return stringBuffer.toString();
	}
	
	public static void main(String[] args) {
		CardDeck cardDeck = new CardDeck();
		System.out.println(cardDeck.toString());
	}
}

output:

Ace of Diamonds
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Spades
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
King of Spades
Ace of Hearts
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Clubs
2 of Clubs
3 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Member Avatar for glebovg

This is what I got:

public class CardDeck
{
	public static void main(String[] args)
	{
	String[] deck=new String[52];
	createDeck(deck);
	for(int i=0; i<52; i++)
	System.out.println(deck[i]);
	} //end main
	public static void createDeck(String[] myDeck)
	{
	String[] num={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"}; // assign content to string
	int count=0; // initialize count
	for(int x=0; x<13; x++)
{
		myDeck[count]=num[x]+" of Diamonds"; // diamonds
	count++;
}
	for(int x=0; x<13; x++)
{
		myDeck[count]=num[x]+" of Spades"; // spades
	count++;
}
	for(int x=0; x<13; x++)
{
		myDeck[count]=num[x]+" of Hearts"; // hearts
	count++;
}
	for(int x=0; x<13; x++)
{
		myDeck[count]=num[x]+" of Clubs"; // clubs
	count++;
}
	} // end createDeck
} // end class CardDeck
Member Avatar for ztini

Great!

You could also use

myDeck[count++]=num[x]+" of Clubs"; // clubs

instead of

myDeck[count]=num[x]+" of Clubs"; // clubs
	count++;

You should also avoid concatenating variables or method calls with +. Such as:

String foo = getFoo();
String rawr = foo + "bar";

It is very inefficient for the compiler to process this. In this case, you should use StringBuffer (for synchronous execution) or StringBuilder (for asynchronous execution).

new StringBuffer(getFoo()).append("bar");
new StringBuilder(getFoo()).append("bar");

However, if you are just concatenating static text, such as "foo" and "bar". It is very efficient to use:

String rawr = "foo" + "bar";

The compiler simply interprets this as:

String rawr = "foobar";

The last option is MessageFormat which can be used to concatenate and simultaneously format text.

For example:

String rawr = "foo " + "bar " + new SimpleDateFormat("MM-dd-yy").format(new Date());

Compared to:

String rawr = MessageFormat.format("{0} {1} {2,date,MM-dd-yy}", "foo", "bar", new Date());

Both output:

foo bar 03-01-12

Food for thought. Never too early to learn these fundamentals.

Member Avatar for glebovg

Thank you!

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.