• 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

Trying to add objects to an ArrayList

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure how to add objects to an ArrayList. I am going to show some of my code, and could you please tell me, or lead me, in the right direction for how to do this. Thank you!

Okay, that was my object. Now, do I need to somehow return the rank and suit in my while loop? Okay, here is the BridgeHand class that neesds to get the cards added to it. (I thought it would be a good idea if the hand was in an ArrayList of Card objects...)

now, when I try to print the size of my list, it only says '1', so I know I am not doing this correctly, but I keep getting stumped at how to do it...
Sorry for such a long post, but I felt all the code was necessary for someone to try and help me out. Thanks a bunch ahead of time!!
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. There seem to be a bunch of problems, here.
The most important thing to me is that you don't seem to have built your class for testing, and you don't seem to have tested any of the operations independently from the whole operation.
My top tip is to think about how you would test the basic operations of the "Card" class, without needing a "BridgeHand", then write some tests (for example using JUnit) which prove that it works as expected.
Let's look at a few of the specific issues.
I'm quite worried that you have duplicate data in your Card class:

This is always a bad idea, and in this case it's actually dangerous. Can you explain why you put two copies of the rank, and two copies of the suit into this class?
The particular problem with this occurs with the difference between creating an object of this class (sets "rank" and "suit"), and reading one in (sets "cardRank" and "cardSuit"). Couple this with the "getters", which return "card" and "suit", and you should see that calling your "read" method will have no effect on the returned rank and suit from the "getters".
I strongly suggest that you simply delete "cardRank" and "cardSuit", and always use "rank" and "suit".
As for your problems with reading multiple cards, it depends a lot on the contents of the input file. Can you show it to us?
I also notice that you have diagnostic printouts for the rank and suit values read from the file - do these show the correct sequence when you read your file, or does it stop after the first one?
Do you get any IOExceptions thrown back from any of these methods?
 
Jonathan King
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, this is my data file. I only have one line in it so far, because I wanted to make sure my tokenizer even worked.

I get no exceptions, and my diagnostic prints work out fine. I declared the cardRank and the cardSuit, because I wasn't sure how to do it with just rank and suit for my card object.
This is my first time trying to use tokenizer, and trying to use an ArrayList, that is why I am having so many problems.
I consider myself a HUGE beginner, because I have only written about 4 other programs in java... but I felt I would get better help in this area in intermediate. Thank you for your help!
Jon
 
Jonathan King
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and my only test program is a BridgeHandTest.java
I am only writing code so far that I can get my objects into the ArrayList. After I can get that to work out, I plan on writing additional code to do things with the whole BridgeHand (like figure out points, etc.) But since I am a beginner with this whole List thing, I was trying to do it little bits at a time like this.
Here is the BridgeHandTest file:

[ February 01, 2004: Message edited by: Jonathan King ]
 
Jonathan King
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question on this topic... (sorry for all the code taking up most of this post)
It seems to me that I would have to place these cards into the ArrayList inside of my while loop. However, I want to keep my card object as a separate class then my bridge hand, right? And so I would need to take my card objects and use my BridgeHand class to place them into an ArrayList from there. At least that is my thinking of proper Java coding etiquette...
Am I right in assuming this?
Jon
 
reply
    Bookmark Topic Watch Topic
  • New Topic