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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

;JOOP!
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!
radarshCommented:
>> Now it's obvious that 'dealt' and 'i' are only valid within the scope of the for()

'dealt' and 'count'

________
radarsh
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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

can be removed too
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.

CEHJCommented:
That's not a compiler error - it's a runtime one. You need to recompile all classes

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
doc_ufoAuthor Commented:
thanks alot. The error was fixed after i recompiled all my files!
CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.