Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Cannot Find Symbol error

    Hello. I'm working on a program to calculate statistics from a random event. On compiling, I get Cannot Find Symbol errors. The three relevant classes (DiceThree, PairOfDice, and StatCalc2) all reside in the same folder on my harddrive, the same folder from which the command prompt is currently working.

    Here is the code:
    Java Code:
    /*  This program uses the PairOfDice class to roll a virtue pair of dice to see how many times the dice must be rolled before a given face value turns up.  The process is repeated 1000 times.  The StatCalc class is then used to report statistics on the number of rolls needed to show a given amount.  Each possible face vlaue of the dice is tested.
    */
    
    public class DiceThree {
    	public void diceStats(int num) {
    		int faceValue = 0;
    		int countRolls = 0;
    		int target = num;
    
    		for (int i = 0; i < 1001; i++) {
    			do {
    				dice.roll();
    				faceValue = dice.getTotal();
    				countRolls++;
    			} while (faceValue != target);
    	
    			stat.enter(countRolls);
    			
    			countRolls = 0;
    		}	
    	}
    		
    	public static void main(String[] args) {
    
    		DiceThree dt;
    		dt = new DiceThree();
    
    		PairOfDice dice; 
    		dice = dt.new PairOfDice();
    
    		StatCalc2 stat;
    		stat = dt.new StatCalc2(); 
    
    		for (int i = 2; i < 13; i++) {
    			dt.diceStats(i);
    
    			System.out.println("For a face value of " + i + ":");
    			System.out.println("Minimum number of rolls: " + stat.getMin());
    			System.out.println("Maximum number of rolls: " + stat.getMax());
    			System.out.println("Average number of rolls: " + stat.getMean());
    			System.out.println("Standard deviation of the number of rolls: " + stat.getStandardDeviation());
    			System.out.println();
    		}	
    	}
    }
    The error messages say Cannot Find Symbol for:
    dice.roll();
    dice.getTotal();
    stat.enter(countRolls);
    dice = dt.new PairOfDice();
    stat = dt.new StatCalc2();

    Any suggestions for helping me figure this out are appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Cannot Find Symbol error

    That's because the diceStats method has no clue about the "local" instantiations of the classes inside of the main method. You need to make those local class variables instance variables.

    Regards,
    Jim
    Last edited by jim829; 04-23-2013 at 05:36 PM. Reason: more precise wording
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Cannot Find Symbol error

    Hello. Thanks for your helpful reply.

    Let me see if I understand correctly: so the "PairOfDice dice;" and the "StatCalc2 stat;" declarations and assignments should be made in the diceStats method? Is that correct?

    Thanks.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Cannot Find Symbol error

    Nope. It would work if diceStats was the only method that used them. But not only does diceStats use them but your main method also needs to reference the StatCalc2 instance. So I would make them instance fields and place them between the DiceThree class definition and the diceStats method signature. Also, once you do this you will need a reference of DiceThree to reference the stat reference in your main method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Cannot Find Symbol error

    Hello. There are many aspects of the Class and Object concepts that I do not understand well.

    Here is how I have modified the program, I am not sure I understood your suggestions correctly. I now get (Identifier) Expected error messages referencing the dice and stat assignments on lines 4 and 7.

    Java Code:
    /*  This program uses the PairOfDice class to roll a virtue pair of dice to see how many times the dice must be rolled before a given face value turns up.  The process is repeated 1000 times.  The StatCalc class is then used to report statistics on the number of rolls needed to show a given amount.  Each possible face value of the dice is tested.
    */
    
    public class DiceThree {
    
    	PairOfDice dice;
    	dice = new PairOfDice();
    	
    	StatCalc2 stat;
    	stat = new StatCalc2();
    	
    	public static void diceStats(int num) {
    		int faceValue = 0;
    		int countRolls = 0;
    		int target = num;
    
    		for (int i = 0; i < 1001; i++) {
    			do {
    				dice.roll();
    				faceValue = dice.getTotal();
    				countRolls++;
    			} while (faceValue != target);
    	
    			stat.enter(countRolls);
    			
    			countRolls = 0;
    		}	
    	}
    		
    	public static void main(String[] args) {
    
    		DiceThree dt;
    		dt = new DiceThree();
    
    		for (int i = 2; i < 13; i++) {
    			dt.diceStats(i);
    
    			System.out.println("For a face value of " + i + ":");
    			System.out.println("Minimum number of rolls: " + dt.getMin());
    			System.out.println("Maximum number of rolls: " + dt.getMax());
    			System.out.println("Average number of rolls: " + dt.getMean());
    			System.out.println("Standard deviation of the number of rolls: " + dt.getStandardDeviation());
    			System.out.println();
    		}	
    	}
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Cannot Find Symbol error

    Java Code:
    public class DiceThree {
    
     
        PairOfDice dice;
        StatCalc2 stat;
       // move class instantiations to constructor
     
       public DiceThree() {
                stat = new StatCalc2();
                dice = new PairOfDice();
        }
    
        public static void diceStats(int num) {
    
            int faceValue = 0;
            int countRolls = 0;
            int target = num;
     
            for (int i = 0; i < 1001; i++) {
                do {
                    dice.roll();
                    faceValue = dice.getTotal();
                    countRolls++;
    
                } while (faceValue != target);
    
         
    
                stat.enter(countRolls);
    
                 
    
                countRolls = 0;
    
            }   
    
        }
    
             
    
        public static void main(String[] args) {
    
     
    
            DiceThree dt;
    
            dt = new DiceThree();
    
     
    
            for (int i = 2; i < 13; i++) {
    
                dt.diceStats(i);
                System.out.println("For a face value of " + i + ":");
    
                System.out.println("Minimum number of rolls: " + dt.stat.getMin());  // changed this
    
                System.out.println("Maximum number of rolls: " + dt.stat.getMax());  // changed this
    
                System.out.println("Average number of rolls: " + dt.stat.getMean());  // changed this
    
                System.out.println("Standard deviation of the number of rolls: " + dt.stat.getStandardDeviation());  // changed this
    
                System.out.println();
    
            }   
        }
    }
    Ok, I made a few changes (I'm not certain I could have explained them). Hopefully, if the rest of your code is correct, this should work. I couldn't check it out since other classes are missing. Also, you need to review instance vs local variables and instance vs static variables. It is a basic and essential part of using Java.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Cannot Find Symbol error

    Hello. Yes, with your changes, it now compiles and works just as it should.

    (A pair of fair die will show Snake-eyes once every 36 rolls (on average), and Sevens shows up once every 15 rolls (on average). Interestingly, the program tells me that 256 was the maximum number of rolls needed to get each different face value - something that jibes with my understanding of randomness and probability. I probably should go back and check my StatCalc class.).

    Thanks for your help. I appreciate you showing me how to make the DiceThree constructor, and I didn't even know it was acceptable to call a method using dt.stat.getMin() - tho' it makes sense to me know that I see the whole program.

    I certainly need to get more comfortable the concepts of static vs. non-static and local vs. instance variables. I am reviewing the relevant pages in The Java Tutorials at oracle.com now.

    Thanks again.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Cannot Find Symbol error

    You're most welcome!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Error: cannot find symbol
    By ishi07 in forum New To Java
    Replies: 1
    Last Post: 05-24-2012, 05:30 AM
  2. Error: Cannot find symbol
    By surendra0607 in forum New To Java
    Replies: 3
    Last Post: 02-05-2012, 08:13 PM
  3. error: Cannot find symbol
    By iswan in forum AWT / Swing
    Replies: 3
    Last Post: 09-30-2011, 04:02 PM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •