473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 nameOfTheEmployee=input.nextLine(); 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.Scanner;

public class Employee
{
//Initialize variables.
private String employeeName;
private Double employeeHours;
private Double employeeRate;
private Double weeklyPay;

//Constructor
public Employee(String name)
{
employeeName=name;
}

//Employee attributes.

//Employee name methods.
public void setEmployeeName(String name)
{
employeeName=name;
}

public String getEmployeeName()
{
return employeeName;
}

public void displayEmployeeName()
{
System.out.printf("The name of the current employee is %s.\n",getEmployeeName());
}

//Employee hours methods.

public void setEmployeeHours(Double hours)
{
employeeHours=hours;
}

public Double getEmployeeHours()
{
return employeeHours;
}

public void displayEmployeeHours()
{
System.out.printf("The number of hours worked by %s are %.2f.\n", getEmployeeName(), getEmployeeHours());
}

//Employee pay rate methods.

public void setEmployeeRate(Double rate)
{
employeeRate=rate;
}

public Double getEmployeeRate()
{
return employeeRate;
}

public void displayEmployeeRate()
{
System.out.printf("The pay rate for employee %s is $%.2f.\n", getEmployeeName(), getEmployeeRate());
}

//Employee weekly pay methods

public void calculateWeeklyPay()
{
weeklyPay=employeeHours*employeeRate;
}

public void setWeeklyPay(Double pay)
{
weeklyPay=pay;
}

public Double getWeeklyPay()
{
return weeklyPay;
}

public void displayWeeklyPay()
{
System.out.printf("The amount owed to %s for this week is $%.2f.\n", getEmployeeName(), getWeeklyPay());
}

public void calculateMultipleEmployees()
{
Scanner input=new Scanner(System.in);

System.out.print("Please enter the name of the employee and hit enter.");
String nameOfEmployee=input.nextLine();
setEmployeeName(nameOfEmployee);
System.out.println();

while (employeeName!="stop")
{
System.out.print("Please enter the employee's hourly rate:");
Double employeeRate=input.nextDouble();
setEmployeeRate(employeeRate);
System.out.println();

System.out.print("Please enter the number of hours for this week:");
Double employeeHours=input.nextDouble();
setEmployeeHours(employeeHours);
System.out.println();

displayEmployeeName();
displayEmployeeRate();
displayEmployeeHours();
calculateWeeklyPay();
displayWeeklyPay();

System.out.println("Please enter the name of the next employee.");
System.out.println("Or enter \"stop\" and hit enter to quit program:");
String nameOfTheEmployee=input.nextLine();//This line seems to be ignored by the program.
setEmployeeName(nameOfTheEmployee);//I think this one is running because the name changes.
}
}
}//end of Employee class


import java.util.Scanner;

public class EmployeeTest
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);

System.out.println();
System.out.println("Welcome to the Employee Payroll Program.");

Employee newEmployee=new Employee("unassigned");
newEmployee.displayEmployeeName();
System.out.println();

newEmployee.calculateMultipleEmployees();

//System.out.print("Please enter the name of the employee:");
//String nameOfEmployee=input.nextLine();
//newEmployee.setEmployeeName(nameOfEmployee);
//System.out.println();

//System.out.print("Please enter the employee's hourly rate:");
//Double employeeRate=input.nextDouble();
//newEmployee.setEmployeeRate(employeeRate);
//System.out.println();

//System.out.print("Please enter the number of hours for this week:");
//Double employeeHours=input.nextDouble();
//newEmployee.setEmployeeHours(employeeHours);
//System.out.println();

//newEmployee.displayEmployeeName();
//newEmployee.displayEmployeeRate();
//newEmployee.displayEmployeeHours();
//newEmployee.calculateWeeklyPay();
//newEmployee.displayWeeklyPay();

System.out.println("Thank you for using the Employee Payroll Program.");
}
}//end of EmployeeTest class
Mar 19 '07 #1
3 1733
r035198x
13,262 8TB
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 nameOfTheEmployee=input.nextLine(); 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.Scanner;

public class Employee
{
//Initialize variables.
private String employeeName;
private Double employeeHours;
private Double employeeRate;
private Double weeklyPay;

//Constructor
public Employee(String name)
{
employeeName=name;
}

//Employee attributes.

//Employee name methods.
public void setEmployeeName(String name)
{
employeeName=name;
}

public String getEmployeeName()
{
return employeeName;
}

public void displayEmployeeName()
{
System.out.printf("The name of the current employee is %s.\n",getEmployeeName());
}

//Employee hours methods.

public void setEmployeeHours(Double hours)
{
employeeHours=hours;
}

public Double getEmployeeHours()
{
return employeeHours;
}

public void displayEmployeeHours()
{
System.out.printf("The number of hours worked by %s are %.2f.\n", getEmployeeName(), getEmployeeHours());
}

//Employee pay rate methods.

public void setEmployeeRate(Double rate)
{
employeeRate=rate;
}

public Double getEmployeeRate()
{
return employeeRate;
}

public void displayEmployeeRate()
{
System.out.printf("The pay rate for employee %s is $%.2f.\n", getEmployeeName(), getEmployeeRate());
}

//Employee weekly pay methods

public void calculateWeeklyPay()
{
weeklyPay=employeeHours*employeeRate;
}

public void setWeeklyPay(Double pay)
{
weeklyPay=pay;
}

public Double getWeeklyPay()
{
return weeklyPay;
}

public void displayWeeklyPay()
{
System.out.printf("The amount owed to %s for this week is $%.2f.\n", getEmployeeName(), getWeeklyPay());
}

public void calculateMultipleEmployees()
{
Scanner input=new Scanner(System.in);

System.out.print("Please enter the name of the employee and hit enter.");
String nameOfEmployee=input.nextLine();
setEmployeeName(nameOfEmployee);
System.out.println();

while (employeeName!="stop")
{
System.out.print("Please enter the employee's hourly rate:");
Double employeeRate=input.nextDouble();
setEmployeeRate(employeeRate);
System.out.println();

System.out.print("Please enter the number of hours for this week:");
Double employeeHours=input.nextDouble();
setEmployeeHours(employeeHours);
System.out.println();

displayEmployeeName();
displayEmployeeRate();
displayEmployeeHours();
calculateWeeklyPay();
displayWeeklyPay();

System.out.println("Please enter the name of the next employee.");
System.out.println("Or enter \"stop\" and hit enter to quit program:");
String nameOfTheEmployee=input.nextLine();//This line seems to be ignored by the program.
setEmployeeName(nameOfTheEmployee);//I think this one is running because the name changes.
}
}
}//end of Employee class


import java.util.Scanner;

public class EmployeeTest
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);

System.out.println();
System.out.println("Welcome to the Employee Payroll Program.");

Employee newEmployee=new Employee("unassigned");
newEmployee.displayEmployeeName();
System.out.println();

newEmployee.calculateMultipleEmployees();

//System.out.print("Please enter the name of the employee:");
//String nameOfEmployee=input.nextLine();
//newEmployee.setEmployeeName(nameOfEmployee);
//System.out.println();

//System.out.print("Please enter the employee's hourly rate:");
//Double employeeRate=input.nextDouble();
//newEmployee.setEmployeeRate(employeeRate);
//System.out.println();

//System.out.print("Please enter the number of hours for this week:");
//Double employeeHours=input.nextDouble();
//newEmployee.setEmployeeHours(employeeHours);
//System.out.println();

//newEmployee.displayEmployeeName();
//newEmployee.displayEmployeeRate();
//newEmployee.displayEmployeeHours();
//newEmployee.calculateWeeklyPay();
//newEmployee.displayWeeklyPay();

System.out.println("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.
Mar 19 '07 #2
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.
Mar 20 '07 #3
r035198x
13,262 8TB
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
Expand|Select|Wrap|Line Numbers
  1.  if(myString.equals("test")) { 
  2. //....
  3.  
Mar 20 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
5
by: !TG | last post by:
I currently use Do while loop, but I'd rather use a For Loop though I have never gotten the hang of them. Would some one please be so kind as to show me how to loop through a recordset.
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.