Link to home
Start Free TrialLog in
Avatar of doc_ufo
doc_ufo

asked on

compiler unable to find variable

Hi,

I have a little problem in my class, the compiler is unable to find the variable "dealt" although it is declared properly.

class Hand
{
            
            public Hand(){

                        

            }
            public Card dealHand(Pack pack){

                        Card hand[] = new Card[5];
                        
                        
                        for ( int count = 0; count < hand.length; count++ ) {
                                   Card dealt = pack.dealCard();


            if ( dealt != null ) {
                        hand[ count ] = dealt;
                                                System.out.println("dealt = " + dealt);

            }

                 else {
                         System.out.println("NOT ENOUGH CARDS TO DEAL" );
                System.out.println("Shuffle cards to continue" );
                return null;
                 }
                        }
            
            return dealt;
            
            }

}


The error message:
Compiling C:\JPadPro\Documents\Ide\PokerTester\Hand.java
C:\JPadPro\Documents\Ide\PokerTester\Hand.java:40: cannot resolve symbol
symbol  : variable dealt
location: class Hand
            return dealt;

Any one can point out where i've gone wrong?
Avatar of sciuriware
sciuriware

dealt is declared INSIDE a for block and thus unknown at the 'return' statement.

;JOOP!
You could do us and yourself a favour by beginning to type your code a bit formatted;
errors like these will show up sooner:

class Hand
{
         
   public Hand()
   {
   }

   public Card dealHand(Pack pack)
   {
      Card hand[] = new Card[5];
             
      for(int count = 0; count < hand.length; count++ )
      {
         Card dealt = pack.dealCard();

         if(dealt != null )
         {
            hand[ count ] = dealt;
            System.out.println("dealt = " + dealt);
         }
         else
         {
            System.out.println("NOT ENOUGH CARDS TO DEAL" );
            System.out.println("Shuffle cards to continue" );
            return null;
         }
      }
      return dealt;
   }
}

Now it's obvious that 'dealt' and 'i' are only valid within the scope of the for()

;JOOP!
>> Now it's obvious that 'dealt' and 'i' are only valid within the scope of the for()

'dealt' and 'count'

________
radarsh
You are right.
;JOOP!
Avatar of CEHJ
Still not compilable unfortunately. Should be


   public Card dealHand(Pack pack)
   {
      Card hand[] = new Card[5];
      Card dealt = null;
             
      for(int count = 0; count < hand.length; count++ )
      {
         dealt = pack.dealCard();

         if(dealt != null )
         {
            hand[ count ] = dealt;
            System.out.println("dealt = " + dealt);
         }
         else
         {
            System.out.println("NOT ENOUGH CARDS TO DEAL" );
            System.out.println("Shuffle cards to continue" );
            return null;
         }
      }
      return dealt;
   }
>>return null;

can be removed too
Avatar of doc_ufo

ASKER

>>  public Card dealHand(Pack pack)
>>   {
>>      Card hand[] = new Card[5];
>>      Card dealt = null;
>>             
>>      for(int count = 0; count < hand.length; count++ )
>>      {
>>         dealt = pack.dealCard();
>>
>>         if(dealt != null )
>>         {
>>            hand[ count ] = dealt;
>>            System.out.println("dealt = " + dealt);
>>         }
>>         else
>>         {
>>            System.out.println("NOT ENOUGH CARDS TO DEAL" );
>>            System.out.println("Shuffle cards to continue" );
>>         }
>>      }
>>      return dealt;
>>   }

I've tried this and the compiler now gives me this error:

java.lang.NoSuchMethodError: Hand.dealHand(LPack;)Ljava/lang/String;
      at Dealer.Deal(Dealer.java:15)
      at PokerTester.main(PokerTester.java:12)

the Dealer.Deal method is just to call out the dealhand method.

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doc_ufo

ASKER

thanks alot. The error was fixed after i recompiled all my files!
:-)