Reputation: 143
I know this has been asked by many people before (and I looked around quite a bit before coming here), but I can't seem to figure out how to fix my code. My task is to make a static makeDeck method that returns an ArrayList containing the 52 cards of a standard deck of cards. I'm fairly new to ArrayLists, so I'm not entirely sure if I'm initializing my ArrayList makeDeck correctly. This is what I have so far:
public class Card
{
private String mySuit;
private int myValue;
public Card( String suit, int value )
{
mySuit = suit;
myValue = value;
}
public String name()
{
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
return cardNames[ myValue - 2 ] + " of " + mySuit;
}
}
public class MainClass
{
public static void displayCards( ArrayList<Card> a )
{
int i;
System.out.println( "Size is " + a.size() );
for ( i = 0 ; i < a.size() ; i++ )
{
Card c = a.get( i );
System.out.println( "#" + (i+1) + ": " + c.name() );
}
}
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=cardNames.length(); a++)
{
for (int b=0; b<= suits.length(); b++)
{
cards.add(new Card(cardNames[a], suits[b])); //I've noticed many people use this to add the card names and suits to the cards ArrayList, and it gives me an error of not being able to turn suits[b] into an integer, so I'm confused as to how it worked for others
}
}
return cards;
}
public static void main( String[] args )
{
System.out.println(makeDeck());
}
If someone could please help me figure out what I'm doing wrong, I would be very grateful. Thank you!
Upvotes: 2
Views: 6884
Reputation: 393801
You are passing two Strings to your Card
constructor. Before you editted out the code of the Card
class, I saw that your constructor expects a String and an int. That's why the constructor call doesn't compile.
In addition, you forgot to return a value in your makeDeck()
method, and your cards
list is local to that method, so you can't access it from your main method, and you don't have a cards()
method anyway.
Your main method should be :
public static void main( String[] args )
{
System.out.println(makeDeck());
}
Your makeDeck()
should have a return cards;
statement in its last line.
And you should either change the constructor of your Card
class or change the argumnets you are passing to it.
With the current implementation of your Card class, you should probably change the Card creation loop to :
for (int a=0; a < suits.length; a++)
{
for (int b=0; b< 13 ; b++)
{
cards.add(new Card(suits[a],b));
}
}
You'll probably have to change the name()
method of Card
though.
Upvotes: 2
Reputation: 609
You need to make makeDeck()
return cards
. Also in your main print statement you meet to call makeDeck()
instead of cards()
In regards to your error string cannot be converted to int
you need to do one of two things:
1) Change the Card class constructor to take in two strings:
Public Card(string value, string suit)
And change int myValue
to string myValue
2) Change your cardName array to be int
instead of string
. You could alternatively Ignore cardNames
all together and change you insert to the following:
cards.add(new Card(suits[b], a+1));
Upvotes: 1
Reputation: 5023
add return
statement that will return your array list.
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=12; a++)
{
for (int b=0; b<=3; b++)
{
cards.add(new Card(cardNames[a], suits[b])); //I've noticed many people use this to add the card names and suits to the cards ArrayList, and it gives me an error of not being able to turn suits[b] into an integer, so I'm confused as to how it worked for others
}
}
return cards;
}
call makeDeck()
from your main method.
public static void main( String[] args ){
ArrayList<Card> cards = makeDeck();
for(Card c : cards){
System.out.println("You can print card member variables here");
}
}
Upvotes: 0
Reputation: 201439
It seems you forgot to return
at the end of your method,
public static ArrayList<Card> makeDeck()
{
ArrayList<Card> cards = new ArrayList<Card>();
String[] cardNames =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};
String[] suits = { "spades", "hearts", "diamonds", "clubs" };
for (int a=0; a<=12; a++)
{
for (int b=0; b<=3; b++)
{
cards.add(new Card(cardNames[a], b)); // <-- if the second Card
// parameter whould be an int.
}
}
return cards;
}
And you should be calling makeDeck()
not cards()
System.out.println(makeDeck());
Upvotes: 0