• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do i add in an arraylist of object ?  RSS feed

 
Wisam Siya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a card game assignment...
so i have an arraylist called cards that has 20 cards and every card has contains 2 objects, suit and the point Value.
I shuffled the deck, now i want to add half of it to player 1 and the rest of the cards goes to the bot or computer.
how can i add the cards to the player one arraylist and have all the information of the cards?
here is my Deck class code :-



and this is the card class code:



the problem i have is this one doesn't work



 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by Doesn't work? You have to provide a lot more details before we can help.

Why are you using Strings for the cards? Can you not use an enumerated type? There is an example in the Java® Language Specification.
 
Wisam Siya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my teacher will not accept it if i used enumerated type, since i haven't took it yet.

the code that i mentioned that doesn't work, i meant that it gives me an error for it

which is

 
Wisam Siya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add half of the deck which is the 10 cards out of 20 cards to a new arraylist which is playerOne
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, the stack trace tells you everything you need to know. Something is null at line 11. Now all you have to do is make sure whatever it is, isn't null any more.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Wisam,
how is your assignment going?
if I understand you correctly, you want to construct a deck of 20 cards( by the way, what kind of cards do you have in mind? if it's the regular cards, then shouldn't the deck has 52 cards?)
then divide it into two equal piles of ten cards and pass it to the user and computer, right?

I did look at the code that deals ten cards to each player and I'm not sure if this is the code that causing you runtime error, however I did notice that you are dealing 9 cards to each player instead of ten..you might want to check that out. better yet why don't you make a loop for 20, size of the deck, and for first ten give it to player and last give it to pc? or another idea would be why not use i%2 to pass cards one by one to each player?

and I want to ask you why did you choose not to create a Player class with it's own array of cards?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!