Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Connection problem in Enum

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connection problem in Enum

    enum Cards {
    ENERGY(6),
    CORVETE(2),
    BATTLE_HAMMER(2),
    BATTLE_CRUISER(2),
    BATTLE_AXE(2),
    COLONY_SHIP(2),
    CONSTRUCTOR(2),
    DEFENDER(2),
    FRIGATE(2),
    SCOUT(2),
    STAR_FIGHTER(2),
    STAR_HAWK(2),
    SURVEY_SHIP(2),
    TRANSPORT(2);


    private int count;
    public int deckRand;

    Cards(int count) {
    this.count = count;

    }

    public int getCount() {return count;}
    }


    class Card {

    public Cards type;

    Card(Cards type){
    this.type = type;
    }

    public Cards getType() {
    return type;
    }
    }

    class CardDeck { // construct a deck
    List<Card> deck;
    CardDeck() { // constructor
    deck = new ArrayList<Card>();
    for (Cards type : Cards.values()) {
    for(int i=0;i<type.getCount();i++){
    deck.add(new Card(type));

    }
    }
    System.out.println(deck);
    Collections.shuffle(deck);
    for (Card element : deck)
    System.out.println(element);
    }
    }

    public class GalCivMain extends JFrame {.......................



    I have here an enum. In main class I need to find out if the card on FE. 12th place is ENERGY or not. And I also don't know, how to write it (I mean how to write if the card on 12th place is ENERGY, do sth.. Could anyone help me pls? Thanks for reacting.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,324
    My Mood
    Hungover
    Thanks
    142
    Thanked 624 Times in 535 Posts

    Default Re: Connection problem in Enum

    Which part of this is giving you trouble? Seeing what is stored in a specific index? Comparing two emum values for equality? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    I don't know how to reffer to the enum. For example, I have one chosen card and in my main class I need to find out, which type is chosen. FE if card chosen is ENERGY, do sth. If not, do sth else. But I don't know how to reffer to the enum.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    ok, I'll give you an example. I wrote there Collections.shuffle. I just need to check if the deck is shuffled. I wanted to write System.out.println(" and here is written the name of the card, but sth like "galciv.Card@7a3570b0" but I need to see here just "ENERGY" - the concrete name of the card"). Do you understand?

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    ok, I'll give you an example. I wrote there Collections.shuffle. I just need to check if the deck is shuffled. I wanted to write System.out.println(" and here is written the name of the card, but sth like "galciv.Card@7a3570b0" but I need to see here just "ENERGY" - the concrete name of the card"). Do you understand? :-)

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    ok, I'll give you an example. I wrote there Collections.shuffle. I just need to check if the deck is shuffled. I wanted to write System.out.println(" and here is written the name of the card, but sth like "galciv.Card@7a3570b0" but I need to see here just "ENERGY" - the concrete name of the card"). Do you understand?

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    Does it work?

    --- Update ---

    I have there Collections.shuffle. I need to check, if the deck is really suffled, so I want to add there System.out.println("and here the name of the each cards"). But if I do it, I just get value like "galciv.Card@6627e353". But I need there value like "ENERGY" - the real name of the card.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,324
    My Mood
    Hungover
    Thanks
    142
    Thanked 624 Times in 535 Posts

    Default Re: Connection problem in Enum

    You're already referring to the enum, when you iterate over its values:

    for (Cards type : Cards.values()) {

    To refer to a specific enum instance, you just use the enum name followed by the enum instance name: Cards.ENERGY, for example.

    For more info, check out the enum tutorial: Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    --- Update ---

    I'm really not sure what you're trying to do. I suggest you put together an SSCCE that demonstrates exactly, and only, what you're trying to do. Posting your whole program without the code you're actually confused about makes it harder to give you good answers.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    I just put only the beginning of the program, the program itself has over 400 lines. I have there Collection.shuffle - I want to shuffle the deck. To check out, if it is really shuffled, I want to add there System.out.println("here the name of each card"). I got value like "galciv.Card@6627e353" but I in fact want sth like "ENERGY" - the real name of the card.

  10. #10
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    OMG, I can't even send a message here: it write "Thank you for posting! Your post will not be visible until a moderator has approved it for posting.

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    I just put only the beginning of the program, the program itself has over 400 lines. I have there Collection.shuffle - I want to shuffle the deck. To check out, if it is really shuffled, I want to add there System.out.println("here the name of each card"). I got value like "galciv.Card@6627e353" but I in fact want sth like "ENERGY" - the real name of the card.

  12. #12
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connection problem in Enum

    I just put only the beginning of the program, the program itself has over 400 lines. I have there Collection.shuffle - I want to shuffle the deck. To check out, if it is really shuffled, I want to add there System.out.println("here the name of each card"). I got value like "galciv.Card@6627e353" but I in fact want sth like "ENERGY" - the real name of the card. :-)

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,324
    My Mood
    Hungover
    Thanks
    142
    Thanked 624 Times in 535 Posts

    Default Re: Connection problem in Enum

    Like I said, it's pretty hard to help you without an SSCCE. Keep in mind that an SSCCE should be as short as possible and only contain the relevant code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Problem in connection
    By jitendm in forum JDBC & Databases
    Replies: 1
    Last Post: July 26th, 2012, 09:00 AM
  2. problem with enum (int to months)
    By mickey2012 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 19th, 2012, 10:59 PM
  3. Help with simple enum problem
    By Prox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2011, 02:19 PM
  4. Declaring enum problem
    By kumalh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 5th, 2011, 06:30 AM
  5. [SOLVED] Enum problem
    By pajfilms in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2011, 07:26 AM