Hello everybody!

I am new to the site and to java. I am in week 2 of my java class now and still not sure if I am enjoying it. Unfortunately I would say it is my weakest link in the IT field so I picked this class on my own. So far the minor problems have proven to be frustrating.

So I think I have the majority of this program correct and can not figure out why I keep getting error "cannot find symbol variable PayrollProgram1" in line 18. I have fixed the other errors that I was having so far but do not see where this error is coming into play. I have it spelled the same throughout my code and do not understand what else could be wrong with it. I am curious at to everybody's input and thank you in advance for it!

import java.util.Scanner; // load scanner

public class PayrollProgram1 // set public class
{
	public static void main( String[] args ) // main method begins
	{
	Scanner input = new Scanner( System.in ); // create scanner to get input
	
	int hoursWorked; // hours worked
	int hourlyPay; // hourly pay
	int weeklyPay; // weekly pay
		
	System.out.print( "Enter Employee's Name." ); // enter employee's name
	String empName = input.nextLine().
		PayrollProgram1.setEmpName ( empName );
		
	System.out.print( "Enter hours worked." ); // enter hours worked
	hoursWorked = input.nextInt();
	
	weeklyPay = hoursWorked * hourlyPay; // multiply hourlyPay by hoursWorked for weeklyPay
	System.out.printf( "Employee Name: %s, Weekly Pay is $%d%n", empName, weeklyPay ); // print final line
	}
}

Recommended Answers

All 3 Replies

Your method setEmpName(

PayrollProgram1.setEmpName ( empName );

need to be a static method.

import java.util.Scanner; // load scanner

public class PayrollProgram1{ // set public class

    public static void setEmpName(String empName) {
        System.out.println(empName);  // EXAMPLE
    }

    public static void main(String[] args){ // main method begins    
        Scanner input = new Scanner(System.in); // create scanner to get input
        int hoursWorked; // hours worked
        int hourlyPay = 10; // hourly pay  // EXAMPLE
        int weeklyPay; // weekly pay
        
        System.out.print("Enter Employee's Name."); // enter employee's name
        String empName = input.nextLine();
        PayrollProgram1.setEmpName(empName);

        System.out.print("Enter hours worked."); // enter hours worked
        hoursWorked = input.nextInt();

        weeklyPay = hoursWorked * hourlyPay; // multiply hourlyPay by hoursWorked for weeklyPay
        System.out.printf("Employee Name: %s, Weekly Pay is $%d%n", empName, weeklyPay); // print final line
    }
}

second little mistake is, that you connected two lines in one using dot:

String empName = input.nextLine().PayrollProgram1.setEmpName ( empName ); ///??????

Lines 16 and 17.

Also the best way to do this is create a separate class Employee.

This may compile and run but you should never do it like this:

public static void setEmpName(String empName) {
   System.out.println(empName); // EXAMPLE
}

The empName is the name of the employee when on the other hand the PayrollProgram1 is the class where you run your program, so they need to be separate. quuba gave you this advise because probably it was the best way to correct this kind of code. And if you take a better look at your code you will see that the way you called that method was completely unnecessary

Here is an example:
1st class:

class Employee {
private String empName=null;
private int hoursWorked = 0;
private int hourlyPay = 0;

public Employee() {

}

public String getEmpName() {
  return empName;
}

public void setEmpName(String empName) {
  this.empName=empName;
}

//generate the rest of the get/set methods

public int getWeeklyPay () {
   return hoursWorked * hourlyPay;
}
import java.util.Scanner; // load scanner

public class PayrollProgram1{ // set public class
    public static void main(String[] args){ // main method begins    
        Scanner input = new Scanner(System.in); // create scanner to get input
       
        Employee empl = new Employee();
        empl.setHourlyPay(10);

        System.out.print("Enter Employee's Name."); // enter employee's name
        empl.setEmpName(input.nextLine());

        System.out.print("Enter hours worked."); // enter hours worked
empl.setHoursWorked(input.nextInt();

        System.out.printf("Employee Name: %s, Weekly Pay is $%d%n", empl.getEmpName(), empls.getWeeklyPay()); // print final line
    }
}

You might think that there isn't much difference but later you will see that this approach has numerous advantages when you will be asked to build more complex programs with multiple employees

I would like to thank both of you for the help. It was very informative and after playing around a little bit ( left a little bit out like decimals and stuff) I got everything working correctly. this stuff seems to be getting a little easier to understand after actually writing a program or two. I was pretty lost when I started this project, but after looking over what you guys provided and messing around a little bit, things seem like they are beginning to come together.Anyways like I said, I just wanted to thank you guys for the responses!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.