I am sure you guys have seen this before. I am working on the famous Employee Payroll program for a class assignment. In any case, the while loop at the end of the Employee class just keeps looping; even after I put in code asking for the employee name again. it just ignores the
String nameOfTheEmploy ee=input.nextLi ne(); and starts the loop over again. What am I doing wrong? below is the code. You will note that the code has slowly evolved from previous versions, I hope you guys can understand.
import java.util.Scann er;
public class Employee
{
//Initialize variables.
private String employeeName;
private Double employeeHours;
private Double employeeRate;
private Double weeklyPay;
//Constructor
public Employee(String name)
{
employeeName=na me;
}
//Employee attributes.
//Employee name methods.
public void setEmployeeName (String name)
{
employeeName=na me;
}
public String getEmployeeName ()
{
return employeeName;
}
public void displayEmployee Name()
{
System.out.prin tf("The name of the current employee is %s.\n",getEmplo yeeName());
}
//Employee hours methods.
public void setEmployeeHour s(Double hours)
{
employeeHours=h ours;
}
public Double getEmployeeHour s()
{
return employeeHours;
}
public void displayEmployee Hours()
{
System.out.prin tf("The number of hours worked by %s are %.2f.\n", getEmployeeName (), getEmployeeHour s());
}
//Employee pay rate methods.
public void setEmployeeRate (Double rate)
{
employeeRate=ra te;
}
public Double getEmployeeRate ()
{
return employeeRate;
}
public void displayEmployee Rate()
{
System.out.prin tf("The pay rate for employee %s is $%.2f.\n", getEmployeeName (), getEmployeeRate ());
}
//Employee weekly pay methods
public void calculateWeekly Pay()
{
weeklyPay=emplo yeeHours*employ eeRate;
}
public void setWeeklyPay(Do uble pay)
{
weeklyPay=pay;
}
public Double getWeeklyPay()
{
return weeklyPay;
}
public void displayWeeklyPa y()
{
System.out.prin tf("The amount owed to %s for this week is $%.2f.\n", getEmployeeName (), getWeeklyPay()) ;
}
public void calculateMultip leEmployees()
{
Scanner input=new Scanner(System. in);
System.out.prin t("Please enter the name of the employee and hit enter.");
String nameOfEmployee= input.nextLine( );
setEmployeeName (nameOfEmployee );
System.out.prin tln();
while (employeeName!= "stop")
{
System.out.prin t("Please enter the employee's hourly rate:");
Double employeeRate=in put.nextDouble( );
setEmployeeRate (employeeRate);
System.out.prin tln();
System.out.prin t("Please enter the number of hours for this week:");
Double employeeHours=i nput.nextDouble ();
setEmployeeHour s(employeeHours );
System.out.prin tln();
displayEmployee Name();
displayEmployee Rate();
displayEmployee Hours();
calculateWeekly Pay();
displayWeeklyPa y();
System.out.prin tln("Please enter the name of the next employee.");
System.out.prin tln("Or enter \"stop\" and hit enter to quit program:");
String nameOfTheEmploy ee=input.nextLi ne();//This line seems to be ignored by the program.
setEmployeeName (nameOfTheEmplo yee);//I think this one is running because the name changes.
}
}
}//end of Employee class
import java.util.Scann er;
public class EmployeeTest
{
public static void main(String args[])
{
Scanner input=new Scanner(System. in);
System.out.prin tln();
System.out.prin tln("Welcome to the Employee Payroll Program.");
Employee newEmployee=new Employee("unass igned");
newEmployee.dis playEmployeeNam e();
System.out.prin tln();
newEmployee.cal culateMultipleE mployees();
//System.out.prin t("Please enter the name of the employee:");
//String nameOfEmployee= input.nextLine( );
//newEmployee.set EmployeeName(na meOfEmployee);
//System.out.prin tln();
//System.out.prin t("Please enter the employee's hourly rate:");
//Double employeeRate=in put.nextDouble( );
//newEmployee.set EmployeeRate(em ployeeRate);
//System.out.prin tln();
//System.out.prin t("Please enter the number of hours for this week:");
//Double employeeHours=i nput.nextDouble ();
//newEmployee.set EmployeeHours(e mployeeHours);
//System.out.prin tln();
//newEmployee.dis playEmployeeNam e();
//newEmployee.dis playEmployeeRat e();
//newEmployee.dis playEmployeeHou rs();
//newEmployee.cal culateWeeklyPay ();
//newEmployee.dis playWeeklyPay() ;
System.out.prin tln("Thank you for using the Employee Payroll Program.");
}
}//end of EmployeeTest class
String nameOfTheEmploy ee=input.nextLi ne(); and starts the loop over again. What am I doing wrong? below is the code. You will note that the code has slowly evolved from previous versions, I hope you guys can understand.
import java.util.Scann er;
public class Employee
{
//Initialize variables.
private String employeeName;
private Double employeeHours;
private Double employeeRate;
private Double weeklyPay;
//Constructor
public Employee(String name)
{
employeeName=na me;
}
//Employee attributes.
//Employee name methods.
public void setEmployeeName (String name)
{
employeeName=na me;
}
public String getEmployeeName ()
{
return employeeName;
}
public void displayEmployee Name()
{
System.out.prin tf("The name of the current employee is %s.\n",getEmplo yeeName());
}
//Employee hours methods.
public void setEmployeeHour s(Double hours)
{
employeeHours=h ours;
}
public Double getEmployeeHour s()
{
return employeeHours;
}
public void displayEmployee Hours()
{
System.out.prin tf("The number of hours worked by %s are %.2f.\n", getEmployeeName (), getEmployeeHour s());
}
//Employee pay rate methods.
public void setEmployeeRate (Double rate)
{
employeeRate=ra te;
}
public Double getEmployeeRate ()
{
return employeeRate;
}
public void displayEmployee Rate()
{
System.out.prin tf("The pay rate for employee %s is $%.2f.\n", getEmployeeName (), getEmployeeRate ());
}
//Employee weekly pay methods
public void calculateWeekly Pay()
{
weeklyPay=emplo yeeHours*employ eeRate;
}
public void setWeeklyPay(Do uble pay)
{
weeklyPay=pay;
}
public Double getWeeklyPay()
{
return weeklyPay;
}
public void displayWeeklyPa y()
{
System.out.prin tf("The amount owed to %s for this week is $%.2f.\n", getEmployeeName (), getWeeklyPay()) ;
}
public void calculateMultip leEmployees()
{
Scanner input=new Scanner(System. in);
System.out.prin t("Please enter the name of the employee and hit enter.");
String nameOfEmployee= input.nextLine( );
setEmployeeName (nameOfEmployee );
System.out.prin tln();
while (employeeName!= "stop")
{
System.out.prin t("Please enter the employee's hourly rate:");
Double employeeRate=in put.nextDouble( );
setEmployeeRate (employeeRate);
System.out.prin tln();
System.out.prin t("Please enter the number of hours for this week:");
Double employeeHours=i nput.nextDouble ();
setEmployeeHour s(employeeHours );
System.out.prin tln();
displayEmployee Name();
displayEmployee Rate();
displayEmployee Hours();
calculateWeekly Pay();
displayWeeklyPa y();
System.out.prin tln("Please enter the name of the next employee.");
System.out.prin tln("Or enter \"stop\" and hit enter to quit program:");
String nameOfTheEmploy ee=input.nextLi ne();//This line seems to be ignored by the program.
setEmployeeName (nameOfTheEmplo yee);//I think this one is running because the name changes.
}
}
}//end of Employee class
import java.util.Scann er;
public class EmployeeTest
{
public static void main(String args[])
{
Scanner input=new Scanner(System. in);
System.out.prin tln();
System.out.prin tln("Welcome to the Employee Payroll Program.");
Employee newEmployee=new Employee("unass igned");
newEmployee.dis playEmployeeNam e();
System.out.prin tln();
newEmployee.cal culateMultipleE mployees();
//System.out.prin t("Please enter the name of the employee:");
//String nameOfEmployee= input.nextLine( );
//newEmployee.set EmployeeName(na meOfEmployee);
//System.out.prin tln();
//System.out.prin t("Please enter the employee's hourly rate:");
//Double employeeRate=in put.nextDouble( );
//newEmployee.set EmployeeRate(em ployeeRate);
//System.out.prin tln();
//System.out.prin t("Please enter the number of hours for this week:");
//Double employeeHours=i nput.nextDouble ();
//newEmployee.set EmployeeHours(e mployeeHours);
//System.out.prin tln();
//newEmployee.dis playEmployeeNam e();
//newEmployee.dis playEmployeeRat e();
//newEmployee.dis playEmployeeHou rs();
//newEmployee.cal culateWeeklyPay ();
//newEmployee.dis playWeeklyPay() ;
System.out.prin tln("Thank you for using the Employee Payroll Program.");
}
}//end of EmployeeTest class
Comment