Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Cannot find symbol error in single package program with default access modifier.

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Cannot find symbol error in single package program with default access modifier.

    By my understanding, variables with default access modifiers are supposed to be able to be accessed from anywhere in a package, and if I'm not mistaken, a package means a single file? Or, does a package mean literally, only if it's named as a package at the top of the file?

    The problem is my booleans on line 10. I'm trying to reuse the same boolean variables in my score class and in my main method. But the compile is returning "cannot find symbol" errors for every attempt to use them in my main method.

    // an easy to use program to hold snooker match scores and feed detailed statistics to the user
    import java.util.Scanner;
     
    // class Score _ returns points for pots and fouls
    class Score{  
     
       final private int[] value = {1,2,3,4,5,6,7}; // array of values for ball colour
       int reds = 15, prvb;                          // number of reds used to calculate maximum points remaining on table _ prv holds previous ball potted
       char pact;  // previous action
       boolean swap = true, brk = false;
     
       int point(int clr, char act){                // method receives ball colour/action _ returns points ------------------------------------------------------------------------------------------
     
          int r=0; // reset return value
          for(int i = 1; i<=clr; i++){              // scan input for ball colour
     
             for(int j = 0; j<= act; j++){          // scan input for action type
     
                if(i == clr && j == act){           // when ball colour and action type match parameters/arguments from main method
     
                   if(act == 'p'){                  // pot!
     
     
                      if(brk == true && pact == 'p' && (reds >=1 && (clr !=1 || prvb !=1)) || (prvb == 1 && clr == 1)){     // if brk is true and reds remaining
                         System.out.println("While reds remain on table, you must pot a red and a colour alternately.");    // error message
                         break;
                      }
     
                      r = value[clr-1];             // return value is ball colour -1 in index
     
                      if(clr == 1){                 // if red ball potted, remove red from total reds                                            *** ( POINTS REMAINING FOR ACTIVE BREAK ADDS 7)**
                         reds--;
                      }
                   }
     
     
                   else if(act == 'f'){             // foul!
     
                      // if red ball fouled, ask if red has been potted and reds-- accordingly
     
                      if(clr <4){                   // if the value of ball fouled is less than 4 points
     
                         r = 4;                     // minimim value for foul returned
                      }
                      else{                         // if value of ball fouled is 4 or more
     
                         r = value[clr-1];          // return ball value is ball colour -1
                      }  
                   }
                   else{                            // if action is not pot or foul
                      r = 0;
                   }
                }
     
             }
          }
          prvb = clr;    // this ball colour is previous ball colour on next run
          pact = act;    // this ball action is previous ball action on next run
          return r;
     
     
       }
     
       int pointsRemaining(int clr, char act){                  // method to handle points remaining ----------------------------------------------------------------------------------------------
     
          if(clr == 1 && act == 'p' || act == 'P'){             // if clr potted is red _ break is active and points for black are available  
     
             return reds * 8 + 34;  
          }
          else{
     
             return reds * 8 + 27;
          }
       }   
    }
     
     
     
    // main method ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    public class SnookerStats{                    
     
       public static void main(String args[]){
     
          Scanner sc = new Scanner(System.in);
          boolean concede = false;
     
          int temp, rem, scr1, scr2;
          int clr;    // variable to hold which coloured ball
          char act;   // variable to hold which action 
     
          // create object of class for handling scores                                            
          Score obj = new Score();
     
          // multidimensional array _ frames, balls
          System.out.println("Number of frames to play: ");
          temp = sc.nextInt();
          int[][] blsprfrm = new int [temp][7];          
     
          // shot loop --------------------------------------------------------------------------------------------------------------------------------------------------------------------
     
          while(!concede){
     
             // if last shot was a foul _ ask fouled player if they want to put the other player back in _ if free ball occurred and player puts opponent in, minus one red
             // ask for concede here if points remaining are not enough ---------------
     
             // which ball colour
             System.out.println("Select colour:\n1 : Red\n2 : Yellow\n3 : Green\n4 : Brown\n5 : Blue\n6 : Pink\n7 : Black");
             clr = sc.nextInt();
     
             // which action
             System.out.println("Select from the following options:\nP : Pot\nM : Miss\nS : Safety\nF : Foul");
             act = sc.next().charAt(0);
             act = Character.toLowerCase(act);
     
             // if shot is missed, or safety, or foul _ swap player boolean _ continue loop 
             if(act == 'm' || act == 's' || act == 'f'){
                brk = false;
                swap = !swap;
                temp = obj.point(clr, act);
     
             }
     
     
             // if pot or foul _ collect points to temp and give to opposite player _ if free ball, add one red
             if(act == 'p'){
                brk = true;
                temp = obj.point(clr, act);
                System.out.println(temp);
             }
     
             // collect remaining points 
             rem = obj.pointsRemaining(clr, act);            
             System.out.println(rem + " points remaining.");
     
     
     
     
          }  // while end  
     
          // when frame is conceded _ collect statistics and print ------------------------------------    
       }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Cannot find symbol error in single package program with default access modifier.

    To access variables in another class from a method, the method needs to have a reference to the instance of the class and use that to access the variable:
       void someMethod() {
          TheClass theClass =  new TheClass();
          int localInt =  theClass.someInt;    // get contents of someInt in this instance of TheClass
    Note: It is better to use a get method vs directly accessing the variable:
    int localInt = theClass.getSomeInt(); // use get method

    Also it is better to have all the code that works with the contents of variables to be contained in the same class where the variables are declared instead of having methods in other classes manipulating the variable.

    Another item: method names should be verbs describing what the method does.
    What does the method point do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    FightingIrishman (January 14th, 2022)

Similar Threads

  1. Error: cannot find symbol
    By Gunars in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 20th, 2020, 06:06 PM
  2. [SOLVED] Program compiles with a cannot find symbol error- what is that?
    By kissyfurs in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 10th, 2013, 11:13 PM
  3. Error:Cannot find Symbol
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2013, 06:40 PM
  4. [SOLVED] Compiling Package, cannot find symbol
    By Actinistia in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2012, 02:19 PM
  5. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM