constructor - Why it keeps on telling me "variable card might not have been initialized" -


this java poker game project. @ beginning, defined card class.

class card {  /* constant suits , ranks */ static final string[] suit = {"clubs", "diamonds", "hearts", "spades" }; static final string[] rank = {"","a","2","3","4","5","6","7","8","9","10","j","q","k"};  /* data field of card: rank , suit */ private int cardrank;  /* values: 1-13 (see rank[] above) */ private int cardsuit;  /* values: 0-3  (see suit[] above) */  /* constructor create card */ /* throw myplayingcardexception if rank or suit invalid */ public card(int rank, int suit) throws myplayingcardexception {  if ((rank < 1) || (rank > 13))     throw new myplayingcardexception("invalid rank:"+rank); else         cardrank = rank; if ((suit < 0) || (suit > 3))     throw new myplayingcardexception("invalid suit:"+suit); else         cardsuit = suit; }  /* accessor , tostring */ /* may impelemnt equals(), not used */ public int getrank() { return cardrank; } public int getsuit() { return cardsuit; } public string tostring() { return rank[cardrank] + " " + suit[cardsuit]; } 

then, tried define deck class hold card. problem in deck constructor. keeps on telling me "variable card may not initialized". think have initialized @ beginning of constructor. gives me error?

class decks {  /* used keep track of original n*52 cards */ private list<card> originaldecks;     /* starts n*52 cards deck original deck   */ /* used keep track of remaining cards deal */ /* see reset(): resets dealdecks full deck      */ private list<card> dealdecks;  /* number of decks in object */ private int numberdecks;   /**  * constructor: creates default 1 deck of 52 playing cards in originaldecks ,  *          copy them dealdecks.  *              initialize numberdecks=n  * note: need catch myplayingcardexception card constructor  *       use arraylist both originaldecks & dealdecks  */ public decks() {     // implement method!      arraylist<card> originaldecks = new arraylist<card>(52);     arraylist<card> dealdecks = new arraylist<card>(52);       card card;     (int i=1; i<=3; i++)  {          (int j=1; j<= 13; j++)  {              try{                 card = new card(j,i);                    }catch (myplayingcardexception e){                       system.out.println("myplayingcardexception: "+e.getmessage());                 }             originaldecks.add(card);              }                        }      dealdecks.addall(originaldecks);    }  /**  * constructor: creates n decks (52 cards each deck) of playing cards in  *              originaldecks , copy them dealdecks.  *              initialize numberdecks=n  * note: need catch myplayingcardexception card constructor  *       use arraylist both originaldecks & dealdecks  */ public decks(int n) {     int numberdecks=n ;      card card;       (int m=0; m<n; m++){          (int i=0; i<=3; i++) {               (int j=0; j<= 13; j++)  {                  try{                     card = new card(j,i);                           }                     catch (myplayingcardexception e){                            system.out.println("myplayingcardexception: "+e.getmessage());                     }                 originaldecks.add(card);               }              dealdecks.addall(originaldecks);          }     }                } 

the compiler complaining line ...

            originaldecks.add(card); 

... , correct.

if card constructor throws myplayingcardexception when first try initialize card variable, control pass catch block without card having been initialized. catch block prints message, allows control fall through line in question. can catch exception , print message, in case catch block must throw exception (whether same or different one), or else must somehow initialize card itself.

note, too, if card constructor throws myplayingcardexception on subsequent iterations of inner loop card retain previous value. although in case not left uninitialized, value has not 1 want use @ point.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -