wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes adding objects to an arraylist. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding objects to an arraylist. " Watch "adding objects to an arraylist. " New topic
Author

adding objects to an arraylist.

Rene Rad
Greenhorn

Joined: Feb 10, 2010
Posts: 15
I can not for the life of me figure out what I'm doing wrong when trying to "load the deck".

When I use " deck.add(new Card("Ace","Hearts",11));" it puts in info but not properly. When I print the output it comes out with...

[0] = [Card@de1b8a, Card@1e232b5, Card@16f144c, Card@19da4fc, Card@baa466, Card@17f409c, Card@16acdd1, Card@ee6681, Card@18bbc5a, Card@facf0b, Card@2f0df1, Card@13c6a22, Card@15c07d8, Card@10721b0, Card@134b07e, Card@2f48d2, Card@55d93d, Card@296f76, Card@54c4ad, Card@13c7378, Card@1c0f2e5, Card@8ab708]

When I try "deck.add("Ace","Spades",11);" i get a compile error of "cannot find symbol - method add(java.lang.String,java.lang.String.int)"

Does anyone have a clue or tip that can point me in the right direction? I've tried googling but a lot of crap is for just arrays or the arraylists only pass in one parameter.

Thanks.




pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Rene Rad wrote:

When I use " deck.add(new Card("Ace","Hearts",11));" it puts in info but not properly. When I print the output it comes out with...

[0] = [Card@de1b8a, Card@1e232b5, Card@16f144c, Card@19da4fc, Card@baa466, Card@17f409c, Card@16acdd1, Card@ee6681, Card@18bbc5a, Card@facf0b, Card@2f0df1, Card@13c6a22, Card@15c07d8, Card@10721b0, Card@134b07e, Card@2f48d2, Card@55d93d, Card@296f76, Card@54c4ad, Card@13c7378, Card@1c0f2e5, Card@8ab708]

This is happening because the Card class doesn't have a proper toString() method. You need to fix that by either giving it a toString method or rather than trying to print the array at once, iterate through it with a for loop and print each Card's fields one at at time.

When I try "deck.add("Ace","Spades",11);" i get a compile error of "cannot find symbol - method add(java.lang.String,java.lang.String.int)"

look carefully at how you're handling this add and you'll see it's quite different (and incorrect) from the others.
Rene Rad
Greenhorn

Joined: Feb 10, 2010
Posts: 15
pete stein wrote:
Rene Rad wrote:

When I use " deck.add(new Card("Ace","Hearts",11));" it puts in info but not properly. When I print the output it comes out with...

[0] = [Card@de1b8a, Card@1e232b5, Card@16f144c, Card@19da4fc, Card@baa466, Card@17f409c, Card@16acdd1, Card@ee6681, Card@18bbc5a, Card@facf0b, Card@2f0df1, Card@13c6a22, Card@15c07d8, Card@10721b0, Card@134b07e, Card@2f48d2, Card@55d93d, Card@296f76, Card@54c4ad, Card@13c7378, Card@1c0f2e5, Card@8ab708]


This is happening because the Card class doesn't have a proper toString() method. You need to fix that by either giving it a toString method or rather than trying to print the array at once, iterate through it with a for loop and print each Card's fields one at at time.

When I try "deck.add("Ace","Spades",11);" i get a compile error of "cannot find symbol - method add(java.lang.String,java.lang.String.int)"


look carefully at how you're handling this add and you'll see it's quite different (and incorrect) from the others.



I know the second one is different, just something else I was trying. So are you saying that "deck.add(new Card("Ace","Hearts",11));" is in fact putting the correct information into the ArrayList? Cuz when I inspect it I get this... (see attached image)




[Thumbnail for error.png]

Rene Rad
Greenhorn

Joined: Feb 10, 2010
Posts: 15
Nevermind, my Card Class had a logic error in the constructor. The suit parameter wasn't being passed into the instance variable.

Thanks for your help .
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Rene Rad wrote:
I know the second one is different, just something else I was trying. So are you saying that "deck.add(new Card("Ace","Hearts",11));" is in fact putting the correct information into the ArrayList?

I believe so, since deck is an ArrayList of Card type, you must add a Card objects. Your showDeck() method looks wrong:


The line:
is printing out the whole ArrayList, deck with each iteration. Rather than do that it should print out the i'th item, and ArrayList has a method that should be called here to allow you to do this.
Rene Rad
Greenhorn

Joined: Feb 10, 2010
Posts: 15
Ya I got this...

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: adding objects to an arraylist.
 
Similar Threads
Accessor methods on enum
Error
My sad little program can't seem to count cards right
How to test my code for proper function?
check my code please?