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 10 of 10

Thread: Getting "cannot find symbol" errors when trying to pass variables between methods!

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting "cannot find symbol" errors when trying to pass variables between methods!

    Hey all, I have an assignment due tonight and I think I got it about wrapped up, just a few errors with my variables. Let me know if you guys need additional information to help me figure this out!

    import java.util.Scanner;
    import java.util.Calendar;
    //Program uses class Scanner and Calendar
     
    public class NewCode
    {
      private static Scanner input = new Scanner (System.in);
      private static int firstNmLength;
      private static Calendar dateTime = Calendar.getInstance();
      private static int trigger = 1;
     
      public static void main(String [] args)
      {
        generateReport();
     
        System.exit(0);
      }
     
      public static void generateReport()
      { 
        char cont;
        cont = 'Y';
     
        while(Character.toUpperCase(cont) == 'Y')
        {
     
          setEmployeeName(first);
     
          employeeName.substring(0, firstNmLength);
     
          setHoursWorked(hoursWorked);
     
          while(hoursWorked > 40 || hoursWorked < 5)
          {
            testHoursWorked();
          }
     
          setPayRate(payRate);
     
          while(payRate < 7.25 || payRate > 15)
          {
            testPayRate();
          }
     
          double grossPay= hoursWorked*payRate;
          grossPayTotal= grossPay-grossPayTotal;
     
          set401K();
     
          while(percent401K > 10)
          {
            test401K();
          }
     
          if(trigger == 1)
          {
            formatDollarSign();
          }
          else
          {
            formatNoDollarSign();
          }
     
          input.nextLine();
          System.out.printf("Enter \'Y\' to add another employee or \'N\' to exit: ");
          cont= input.nextLine().charAt(0);
     
        }
     
      }
     
     
     
     
      //begin sub-methods
     
     
     
     
      public static String setEmployeeName()
      {
        System.out.printf("Enter the employee's first and last name: ");
          String first = input.next();
          String last = input.next();
     
          first.length();
     
          return first;
      }
      public static int setHoursWorked(String first)
      {
        System.out.printf("Enter the number of hours worked for %s: ",first);
        hoursWorked = input.nextInt();
     
        return hoursWorked;
      }
      public static int testHoursWorked(int hoursWorked)
      {
        if(hoursWorked > 40)
            {
              System.out.printf("Hours worked cannot exceed 40. Please re-enter: ");
              hoursWorked = input.nextInt();
            }
            else
              if(hoursWorked < 5)
            {
              System.out.printf("Hours worked cannot be less than 5. Please re-enter: ");
              hoursWorked = input.nextInt();
            }
     
            return hoursWorked;
      }
      public static double setPayRate()
      {
         System.out.printf("Enter the employee's hourly pay rate: ");
         double payRate = input.nextDouble();
     
         return payRate;
      }
      public static double testPayRate(double payRate)
      {
        if(payRate < 7.25)
            {
              System.out.printf("Hourly pay cannot be less than $7.25. Please re-enter: ");
              payRate = input.nextDouble();
            }
            else
              if(payRate > 15)
            {
              System.out.printf("Hourly pay cannot exceed $15.00. Please re-enter: ");
              payRate = input.nextDouble();
            }
     
         return payRate;
      }
      public static double set401K()
      {
        System.out.printf("Enter the employee's 401K contribution as a percentage of salary: ");
        double percent401K=input.nextDouble(); 
     
        return percent401K;
      }
     
      public static double test401K()
      {
        System.out.printf("Contribution cannot exceed 10%%. Please re-enter: ");
        double percent401K=input.nextDouble();
     
        double retire401K= (percent401K/100)*grossPay;
        total401K= total401K+retire401K;
     
        return percent401K;
      }
      public static String formatDollarSign(String first, String last, double grossPay, double percent401K)
      {
        payrollExpense += String.format("\n %-30s %8s $%,21.2f %8s $%,19.2f",first +" "+ last," ",grossPay," ",retire401K);
        trigger = 0;
     
        return payrollExpense;
      }
      public static String formatNoDollarSign(String first, String last, double grossPay, double percent401K)
      {
        payrollExpense += String.format("\n %-30s %9s %,21.2f %9s %,19.2f",first +" "+ last," ",grossPay," ",retire401K);
     
        return payrollExpense;
      }
    }


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

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    Sorry about that! Here are the errors:

    28 errors found:
    [line: 32]
    Error: cannot find symbol
    symbol: variable first
    location: class MyCode
    [line: 34]
    Error: cannot find symbol
    symbol: variable employeeName
    location: class MyCode
    [line: 36]
    Error: cannot find symbol
    symbol: variable hoursWorked
    location: class MyCode
    [line: 38]
    Error: cannot find symbol
    symbol: variable hoursWorked
    location: class MyCode
    [line: 38]
    Error: cannot find symbol
    symbol: variable hoursWorked
    location: class MyCode
    [line: 40]
    Error: method testHoursWorked in class MyCode cannot be applied to given types;
    required: int
    found: no arguments
    reason: actual and formal argument lists differ in length
    [line: 43]
    Error: cannot find symbol
    symbol: variable payRate
    location: class MyCode
    [line: 45]
    Error: cannot find symbol
    symbol: variable payRate
    location: class MyCode
    [line: 45]
    Error: cannot find symbol
    symbol: variable payRate
    location: class MyCode
    [line: 47]
    Error: method testPayRate in class MyCode cannot be applied to given types;
    required: double
    found: no arguments
    reason: actual and formal argument lists differ in length
    [line: 50]
    Error: cannot find symbol
    symbol: variable hoursWorked
    location: class MyCode
    [line: 50]
    Error: cannot find symbol
    symbol: variable payRate
    location: class MyCode
    [line: 51]
    Error: cannot find symbol
    symbol: variable grossPayTotal
    location: class MyCode
    [line: 51]
    Error: cannot find symbol
    symbol: variable grossPayTotal
    location: class MyCode
    [line: 55]
    Error: cannot find symbol
    symbol: variable percent401K
    location: class MyCode
    [line: 62]
    Error: method formatDollarSign in class MyCode cannot be applied to given types;
    required: java.lang.String,java.lang.String,double,double
    found: no arguments
    reason: actual and formal argument lists differ in length
    [line: 66]
    Error: method formatNoDollarSign in class MyCode cannot be applied to given types;
    required: java.lang.String,java.lang.String,double,double
    found: no arguments
    reason: actual and formal argument lists differ in length
    [line: 98]
    Error: cannot find symbol
    symbol: variable hoursWorked
    location: class MyCode
    [line: 100]
    Error: cannot find symbol
    symbol: variable hoursWorked
    location: class MyCode
    [line: 154]
    Error: cannot find symbol
    symbol: variable grossPay
    location: class MyCode
    [line: 155]
    Error: cannot find symbol
    symbol: variable total401K
    location: class MyCode
    [line: 155]
    Error: cannot find symbol
    symbol: variable total401K
    location: class MyCode
    [line: 161]
    Error: cannot find symbol
    symbol: variable payrollExpense
    location: class MyCode
    [line: 161]
    Error: cannot find symbol
    symbol: variable retire401K
    location: class MyCode
    [line: 164]
    Error: cannot find symbol
    symbol: variable payrollExpense
    location: class MyCode
    [line: 168]
    Error: cannot find symbol
    symbol: variable payrollExpense
    location: class MyCode
    [line: 168]
    Error: cannot find symbol
    symbol: variable retire401K
    location: class MyCode
    [line: 170]
    Error: cannot find symbol
    symbol: variable payrollExpense
    location: class MyCode

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    The symbols with the "cannot find" error message are the symbols/variable names that the compiler can not find in scope (defined within the same pair of {}s).
    Some common causes:
    misspelled names
    name defined in another scope and not where used.
    class A_Class {
       int classVar; // define at class level;  in Scope for all methods
    	void meth1() {
    	    int aVar;    // define aVar; in Scope only within meth1
           //  classVar in Scope here
    		 //...
    	} // end meth1()
       void meth2() {
    	    //  aVar in NOT in SCOPE here
           //  classVar in Scope here
           //...
    	} //  end meth2()
    } // end class


    Recommendation: Do NOT type in so much code before doing a compile. This long list of errors will be much harder to fix than would a short list when you did a compile early and often.


    BTW Stripping off the source lines from the error messages makes the long list of messages hard to use. You need to copy the full text of the compiler's error message.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    Normally I would compile step by step, but this program is based off a simpler one, only this time we moved code to methods. I did copy everything in the error console. I just removed the location since it had local directory information that I'd rather not have on the internet. I do not see the ^ that you refer to in my software. We are required to use Dr. Java.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    We are required to use Dr. Java.
    Does that mean that you aren't able to get good error messages that show the source line where the error happened? Posting a list of error messages without the source line associated with it won't be very useful.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    I copied the entire error list. I don't know what more I can do. Perhaps there is a setting that isn't on preventing more descriptive error messages?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    The problem with the list of errors is that it does not include the source statement where the error happened.
    See post#4 The source statement is:
     var = 2;

    Can you go through the source and add the error messages as comments at the statements where they happened?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    All those errors relate to the class MyCode and yet you have posted code for NewCode. Please post correct information.
    Improving the world one idiot at a time!

  10. #10
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Getting "cannot find symbol" errors when trying to pass variables between methods!

    Unfortunately, Dr.Java doesn't provide very detailed errors. Due to the line spacing, the only effective way for us to suggest how to fix your code would be to copy and paste it into an IDE.

    Your list of errors concern the class MyCode, however, you posted the class NewCode. It too contains errors (28 in total), so perhaps you renamed your class? In any case, the errors are mostly due to one of two problems. First, when you create a method that has a set of parameters, you must fill in those parameters with the correct data types. For example, in your code you called formatDollarSign(), however, the method you created has is formatDollarSign(String s1, String s1, double d1, double d2). Second, although you did declare your variables throughout your code, you need to keep scope in mind.

    For example:
    public class testClass {
        public void demonstrateScope() {
           double addDouble; 
           while(true) {
                // some stuff
                double myDouble = 5.0;
            }
            addDouble += myDouble; 
        }
     
        public void getMyDouble() {
            return myDouble;
        }     
    }

    The above code will throw 2 errors. First, in demonstrateScope(), although myDouble is declared and initially set to 5.0, it cannot be reached by addDouble since it is outside of the scope it was declared in. Similarly, getMyDouble() will return an error that it cannot find myDouble. Again, it's declared in a different method and outside of its scope. Think about how you would fix the above code and then it will be much easier to fix the errors in your assignment.

Similar Threads

  1. "Error cannot find symbol" "throws BadLocationException"
    By coltson in forum AWT / Java Swing
    Replies: 1
    Last Post: June 30th, 2013, 10:33 PM
  2. "cannot find symbol" error when trying to use the getInt() method.
    By simpson_121919 in forum Collections and Generics
    Replies: 6
    Last Post: February 21st, 2013, 12:48 PM
  3. Replies: 3
    Last Post: January 22nd, 2013, 07:14 AM
  4. ERROR - "CANNOT FIND SYMBOL"
    By abinaya.cs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2013, 07:07 AM
  5. "Cannot find symbol" compilation error
    By collegejavastudent in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 12th, 2011, 05:07 PM