• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding objects to an arraylist.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)

error.png
[Thumbnail for error.png]
card object
 
Rene Rad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya I got this...

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic