Quircky while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rmijares
    New Member
    • Mar 2007
    • 2

    #1

    Quircky while loop

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by rmijares
    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
    1.)Please use code tags when posting code
    2.)Do not compare strings using == or !=. Use the .equals method.
    3.)Find time to read this.

    Comment

    • rmijares
      New Member
      • Mar 2007
      • 2

      #3
      Originally posted by r035198x
      1.)Please use code tags when posting code
      2.)Do not compare strings using == or !=. Use the .equals method.
      3.)Find time to read this.
      Thank you for the response. However, one more question. Is the equals method part of another class? So I have to call it like I would the Scanner? I know these are very simple questions, but I have only been studying Java for two weeks.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by rmijares
        Thank you for the response. However, one more question. Is the equals method part of another class? So I have to call it like I would the Scanner? I know these are very simple questions, but I have only been studying Java for two weeks.
        The equals method is part of every class. The Object class in the java.lang package has an equals method and every class extends from the Object class implicitly, so every class has a .equals method.

        You call it on objects e.g if you have string variable called myString, to test if myString has the same value as the string "test", then you do
        Code:
         if(myString.equals("test")) { 
        //....
        }

        Comment

        Working...