[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

compiler unable to find variable

Posted on 2006-03-25
10
Medium Priority
?
228 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:doc_ufo
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 16292646
dealt is declared INSIDE a for block and thus unknown at the 'return' statement.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16292661
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
 
LVL 10

Expert Comment

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

'dealt' and 'count'

________
radarsh
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 24

Expert Comment

by:sciuriware
ID: 16292697
You are right.
;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16292739
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16292742
>>return null;

can be removed too
0
 

Author Comment

by:doc_ufo
ID: 16292753
>>  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
 
LVL 86

Accepted Solution

by:
CEHJ earned 800 total points
ID: 16292771
That's not a compiler error - it's a runtime one. You need to recompile all classes
0
 

Author Comment

by:doc_ufo
ID: 16292817
thanks alot. The error was fixed after i recompiled all my files!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16292895
:-)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question