Results 1 to 3 of 3
  1. #1
    NewToJava_99 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default How to initialize from a string

    I am very new java and have assignment which i have completed and submitted but know that there has to be an easier way of doing this. I need to create a deck of cards, that contains the suit, description and value for each card. When the constructor is initalized the user can type in each of these values. However is there a way in java to enforce these values from a list ? So when the constructor is initialized only the correct value can be entered.

    I have achieved this through a series of if statements but this appears to be a very inneficient way of doing this and I am certain in can be done from public static final variable. I have searched web and have found some suggested solutions but cannot get it work. My basic code is as follows:

    private String suit;

    private String description;

    private int value;


    /**
    * Constructor for objects of class Card
    */

    public Card(String description,
    String suit,
    int value)
    {


    this.description = description;
    this.suit = suit;
    this.value = value;

    }

    I appreciate any assistance on this.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to initialize from a string

    Place your suits and values in an array or enum and then use loops to initialise the cards.

  3. #3
    NewToJava_99 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to initialize from a string

    Thank you, i will give it a try.

Similar Threads

  1. Replies: 4
    Last Post: 10-24-2011, 09:26 PM
  2. Initialize Vs extends
    By N00Bie in forum New To Java
    Replies: 1
    Last Post: 03-22-2011, 02:58 PM
  3. Initialize a Variable
    By lala in forum New To Java
    Replies: 13
    Last Post: 11-16-2010, 06:51 PM
  4. How to initialize an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:47 PM
  5. I do not know how to initialize the two variables
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •