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

Thread: Errors when compiling with an object

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Errors when compiling with an object

    Alright, I'm sure the problem I'm having is an oversight on my part but I'm not seeing my mistake.

    WorkerDemoWilson.java:13: error: cannot find symbol
            ProductionWorkerWilson pw1 = new prodWorker("Bob Jones", "321-D", "09-20-1980",
                                             ^
      symbol:   class prodWorker
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:17: error: cannot find symbol
            ProductionWorkerWilson pw2 = new prodWorker("James Dean", "333", "09-20-1980",
                                             ^
      symbol:   class prodWorker
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:21: error: cannot find symbol
            ProductionWorkerWilson pw3 = new prodWorker("Aaron Fletcher", "123-D", "09-20-1980",
                                             ^
      symbol:   class prodWorker
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:25: error: cannot find symbol
            ProductionWorkerWilson pw4 = new prodWorker("Ed Robertson", "666-D", "09-20-1980",
                                             ^
      symbol:   class prodWorker
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                        ^
      symbol:   class prodWorker
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                   ^
      symbol:   variable employeeName
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                                 ^
      symbol:   variable employeeNumber
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                                                 ^
      symbol:   variable hireDate
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                                                           ^
      symbol:   variable shift
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                                                                  ^
      symbol:   variable payRate
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:81: error: return outside method
    	            return pw; // Returning the production worker
    	            ^
    11 errors

    public class WorkerDemoWilson
    {
     
       public static void main(String[] args)
            {
            // A normal functioning production worker
            ProductionWorkerWilson pw1 = new prodWorker("Bob Jones", "321-D", "09-20-1980",
                                                                         1, 12.25);
     
            // A production worker with an invalid employee number.
            ProductionWorkerWilson pw2 = new prodWorker("James Dean", "333", "09-20-1980",
                                                                          1, 12.25);
     
            // A production worker with an invalid shift.                        
            ProductionWorkerWilson pw3 = new prodWorker("Aaron Fletcher", "123-D", "09-20-1980",
                                                                              3, 12.25);
     
            // A production worker with an invalid pay rate.
            ProductionWorkerWilson pw4 = new prodWorker("Ed Robertson", "666-D", "09-20-1980",
                                                                             1, -500.00);
     
            // Heres our production worker currently.
            System.out.println(pw1);
            System.out.println();
            System.out.println(pw2);
            System.out.println();
            System.out.println(pw3);
            System.out.println();
            System.out.println(pw4);
            System.out.println();
     
            }
     
            // prodWorker method that creates instances.
            public static ProductionWorkerWilson prodWorker(String name, String num, String date, int shi, double rate)
            {
             try
    		  {
    			if(num.length() < 5 || num.length() > 5)
             {
    				throw new InvalidEmployeeNumber(); // Throws exception for invalid employee numbers
    		   }
            }
    		  catch(InvalidEmployeeNumber e)
    		  {
    			System.out.println(e.getMessage() + num);
    	     }
            try
            {
             if(shi != 1 && shi != 2)
             {
                throw new InvalidShift(); // Throws the exception for an invalid shift number
             }
            }
             catch(InvalidShift e)
             {
                System.out.println(e.getMessage() + shi);
             }
             try
             {
              if(rate < 0)
              {
                throw new InvalidPayRate(); // Throws the exception for an invalid shift number
              }
             }
              catch(InvalidPayRate e)
              {
                System.out.println(e.getMessage() + rate);
              }
             }
     
              // Creating the production worker
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
               {
    	            return pw; // Returning the production worker
               }
     
    }

    public class ProductionWorkerWilson extends EmployeeWilson
    {
       public static final int day_shift = 1; // The day shift (1)
       public static final int night_shift = 2; // The night shift (2)
       private int shift;
       private double payRate; // The hourly pay rate.
     
       /**
          Constructor
       */
       public ProductionWorkerWilson()
       {
          super();
          shift = day_shift;
          payRate = 0.0;
       }
     
       /**
          Overloaded constructor
       */
       public ProductionWorkerWilson(String n, String num, String date, int shi, double rate)
       {
          super(n, num, date);
          shift = shi;
          payRate = rate;
       }
     
       /**
          Accessor method that gets the shift number.
       */
     
       public int getShift()
       {
          return shift;
       }
     
       /**
          Accessor method that gets the hourly pay rate.
       */
     
       public double getPayRate()
       {
          return payRate;
       }
     
       /**
          Mutator method that sets the shift number.
       */
     
       public void setShift(int shi) throws InvalidShift
       {
          // Making sure the shift is either 1 or 2
          if(shi < 1 || shi > 2)
          {
             throw new InvalidShift(); // Throws the invalid shift exception.
          }
          else
          {
             shift = shi; // If it passes the check, it sets the shift accordingly
          }
       }
     
       /** 
          Mutator method that sets the pay rate.
       */
     
       public void setPayRate(double rate) throws InvalidPayRate
       {
          // Checking to see if the payrate is invalid
          if(rate < 0)
          {
             throw new InvalidPayRate(); // Throws the invalid pay rate exception
          }
          else
          {
          payRate = rate; // If it passes the check, it sets the payrate accordingly
          }
         }   
     
          // The toString method
          public String toString()
          {
             String string = super.toString();
     
             string += "\nShift: ";
             if(shift == day_shift)
             {
                string += "1 (Day)";
             }
             else if(shift == night_shift)
             {
                string += "2 (Night)";
             }
             else
             {
                string += "Invalid Shift Number";
             }
             return string += ("\nPay Rate: $" + payRate);  
     
       }

    public class EmployeeWilson {
     
       public String employeeName; // The employee's name.
       public String employeeNumber; // The employee's number, formated as XXX-L.
       public String hireDate; // The date in which the employee was hired.
     
     
       /**
          Constructor
       */
     
       public EmployeeWilson()
       {
          employeeName = "";
          employeeNumber = "";
          hireDate = "";
       }
     
       /**
          Overloaded constructor
       */
     
       public EmployeeWilson(String n, String num, String date)
       {
          //Sets the values of the employeename, employeenumber, and hireDate
          employeeName = n;
          employeeNumber = num;
          hireDate = date;
       }
       /** 
          Accessor method that gets the employee's name
       */
     
       public String getEmployeeName()
       {
          return employeeName;
       }
     
       /**
          Accessor method that gets the employee's numbers
       */
     
       public String getEmployeeNumber()
       {
          return employeeNumber;
       }
     
       /**
          Accessor method that gets the employee's hire date
       */
     
       public String getHireDate()
       {
          return hireDate;
       }
     
       /**
          Mutator method that sets the employee's name with the new name.
       */
     
       public void setEmployeeName(String n)
       {
          employeeName = n;
       }
     
       /**
          Mutator method that sets the employee's number with the new number.
       */
     
       public void setEmployeeNumber(String num) throws InvalidEmployeeNumber
       {
          if(isValidEmpNum(num))
          {
             employeeNumber = num;
          }
          else
          {
             employeeNumber = "";
             throw new InvalidEmployeeNumber(); // Throws the invalid employee number exception
          }  
       }
     
       /**
          Mutator method that sets the employee's hire date with the new hire date.
       */
     
       public void setHireDate(String newHDate)
       {
          hireDate = newHDate;
       }
     
       // isValidEmpNum method checks to see if the employee number is formatted correctly
       private boolean isValidEmpNum(String num)
       {
          // Default boolean status to true
          boolean status = true;
     
          //Making sure the number is correct.
          if(num.length() != 5)
          {
             status = false;
          }
          return status;
        }
     
        // The toString method
        public String toString()
        {
          String string = "Name: " + employeeName + "\nEmployee Number: ";
     
          if(employeeNumber == "")
          {   
             string += "Invalid Employee Number";
          }
          else
          {
             string += employeeNumber;
          }
          return string += ("\nHire Date: " + hireDate);
     
         }

    The idea is that I'm trying to create 4 separate objects that I can then call to print. One will work fine, but the other 3 will not work and throw an exception which has been handled in some exception classes. Demonstrating the exception code.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Errors when compiling with an object

    You've created a method that returns ProductionWilsonWorker objects, but you're calling the method as you'd call a constructor. Instead of this:

    ProductionWorkerWilson pw1 = new prodWorker("Bob Jones", "321-D", "09-20-1980",
    1, 12.25);

    You should be doing something like this:

    ProductionWorkerWilson pw1 = prodWorker("Bob Jones", "321-D", "09-20-1980",
    1, 12.25);

    Notice there is no 'new' in the second version.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors when compiling with an object

    Ohhh. Right. That makes sense. That reduced my compiling errors by quite a bit.

    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                        ^
      symbol:   class prodWorker
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                   ^
      symbol:   variable employeeName
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                                 ^
      symbol:   variable employeeNumber
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                                                 ^
      symbol:   variable hireDate
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                                                           ^
      symbol:   variable shift
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:79: error: cannot find symbol
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeeNumber, hireDate, shift, payRate);
    	                                                                                                  ^
      symbol:   variable payRate
      location: class WorkerDemoWilson
    WorkerDemoWilson.java:81: error: return outside method
    	            return pw; // Returning the production worker
    	            ^
    7 errors

    These are all referring to this block of code:

              // Creating the production worker
    	        ProductionWorkerWilson pw = new prodWorker(employeeName, employeNumber, hireDate, shift, payRate);
               {
    	            return pw; // Returning the production worker
               }

  4. #4
    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: Errors when compiling with an object

    error: cannot find symbol
    symbol: class prodWorker
    The compiler can not find a definition for the class: prodWorker
    Is that the correct name for the class?
    Or is the name: ProductionWorkerWilson
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors when compiling with an object

    Quote Originally Posted by Norm View Post
    The compiler can not find a definition for the class: prodWorker
    Is that the correct name for the class?
    Or is the name: ProductionWorkerWilson
    Duh. Right, I had to modify the class name's mid-progress. I guess I wasn't very thorough with it and didn't change that aswell. Found a few more instances of that problem and fixed them.

    But now heres a question I'm curious about. After my program catches the error and displays the error message. What if I want to display the object back to them so that they can see all of the field information. I could just specifically print the pw object that I know is going to cause the error, but what if I don't know the object but still want it to print back. Is it possible to retrieve the object that has the exception and after my "Invalid X" statement is printed, print back the object that caused it? Or do I have to know what object it is that caused it?

  6. #6
    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: Errors when compiling with an object

    You need to have a variable that has a reference to the object so you can call its methods.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors when compiling with an object

    Ahh. I see. Thanks for the information. It looks like I've got everything.

Similar Threads

  1. Replies: 7
    Last Post: May 8th, 2014, 12:51 PM
  2. Help compiling!
    By NoobOfTheMonth in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 19th, 2013, 07:27 AM
  3. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  4. Compiling
    By yavo in forum Java Theory & Questions
    Replies: 1
    Last Post: December 24th, 2011, 07:42 AM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM