Simple Java Question

Discussion in 'Mac Programming' started by ScKaSx, Jan 26, 2008.

  1. ScKaSx macrumors regular

    Feb 27, 2006
    Hi All,

    I was following the Really Big Index Tutorial and was looking at the section on 'Enums'. I have a question about using a set of classes defined in that section for a deck of cards. The following four classes are defined (Deck.class, Card.class, Suit.class, Rank.class):

    I want to create a main program ( that creates a deck and lists the cards in the deck. How do I incorporate Card.toString to achieve this? Thanks.

  2. Pring macrumors 6502

    Sep 17, 2003
    You need a getCards() method in Deck which returns the array of cards. You can then do
    Card[] cards = deck.getCards();
    for (Card card: cards)
    	System.out.println(card); //or card.toString(), but that's implicit when you call println on any object
  3. ScKaSx thread starter macrumors regular

    Feb 27, 2006
    Thanks Pring,

    I added the getCard function to my as:

    And now my looks like this,

    It compiles but I get the following error when I execute:

    Any ideas, thanks.

  4. Thom_Edwards macrumors regular

    Apr 11, 2003
    Just at a quick glance, I would look at when you use cards[i++] = newCard(rank, suit);. You give cards[] 52 indices, but the first card you make is cards[1] (because of i++). So, your last card is cards[52], but that is really the 53rd index since cards[] is zero-based. (cards[0] is the first card, cards[1] is the second, etc.) cards[51] is your last card, so cards[52] is out of bounds.

    Try this:
    cards[i] = new Card(rank, suit);
    This way, the first time you go through the loop, i is still zero. Then you can increment i.

    Like I said, that is just a quick glance. I may be way off.
  5. therevolution macrumors 6502

    May 12, 2003
    Actually, 'i++' increments i after executing the statement. '++i', on the other hand, would do what you're describing. So I don't think that's the problem.

    That said, I don't know why it's going out of bounds. Just thought I'd comment on the preincrement/postincrement thing. :D
  6. Pring macrumors 6502

    Sep 17, 2003
    I think I know what the problem is but I'm going to do the whole teacher thing and teach you how to debug for yourself rather than giving you the answer :)

    Put some debug in your code.

    Just before you instantiate your Card object in the loops in the deck put
    System.out.println(i + ": Suit = " + suit + " Rank = " + rank) ;
    cards[i++] = new Card(rank, suit);
    This will print out to standard out exactly what card it's creating at each point.. eg "0: Diamonds, Deuce", "1: Diamonds, Three" and so on.

    What you then have to do is find out what card it's trying to create before it throws the Exception. From there you should be able to see why it's failing on that card.
  7. toddburch macrumors 6502a

    Dec 4, 2006
    Katy, Texas
    I see the problem too.

    Also, ScKaSx, instead of using [quote] & [/quote] tags for your code, you should be using [code] & [/code] tags for your code. It will preserve your indentation, which, as it happens, would give you a visual clue as to the problem.

  8. ScKaSx thread starter macrumors regular

    Feb 27, 2006
    Thanks for your help guys, its working brilliantly now!

    And Pring thanks for the debugging advice, I much prefer the the method over the answer. I had tried to do something similar but must have had the syntax wrong. Thanks again.

  9. Pring macrumors 6502

    Sep 17, 2003
    No problem, glad you got it working.

    it's an important lesson to remember for anyone learning to program properly. In order to debug you have to understand the flow of your program, one of the best ways to do this is to sprinkle output statements around your code to print the state of variables at different points. This is one of the most effective ways to find out what exactly is going wrong.
  10. Thom_Edwards macrumors regular

    Apr 11, 2003
    That is something I didn't know! I guess I never really even thought about it. I've always done it the way I suggested because of my ignorance. Thanks for the correction. Learn something new every day...
  11. EdC macrumors newbie

    Jan 29, 2008
    To take this a step further, you are in a unique situation to know how many objects are supposed to be in that array at all. Down the road, you should eliminate the dependency on the number 52. I'd recommend creating an ArrayList object. Add as many things to it as you like. At the end of the loop, use the ArrayList object to get a real array of the objects. You won't have to worry about how many objects your loop provides and your code is more flexible.

    Before anyone else says it, this suggestion is *way* overkill for what you are trying to do here. I'm just trying to give you ideas for growth, e.g. you could reuse the algorithms to get all the cards in an Uno deck or a Magic deck or something.

    Lastly, I would always tell my engineers to move the i++ to the next line outside of the array index braces. It will reduce the chance that you will do something wrong, i.e. less hours to debug your program on average. When you are starting out, System.out.println() works wonders.


Share This Page