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?
doc_ufoAsked:
Who is Participating?
 
CEHJCommented:
That's not a compiler error - it's a runtime one. You need to recompile all classes
0
 
sciuriwareCommented:
dealt is declared INSIDE a for block and thus unknown at the 'return' statement.

;JOOP!
0
 
sciuriwareCommented:
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!
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

'dealt' and 'count'

________
radarsh
0
 
sciuriwareCommented:
You are right.
;JOOP!
0
 
CEHJCommented:
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;
   }
0
 
CEHJCommented:
>>return null;

can be removed too
0
 
doc_ufoAuthor Commented:
>>  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.

0
 
doc_ufoAuthor Commented:
thanks alot. The error was fixed after i recompiled all my files!
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.