Hello,

The following is background on my assignment "Modify payroll program so that it uses a class to store and retrive the employee's name, hourly rate and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Make sure the code is readable and well documented."

I have everything coded but my while loop for my program exit value is not working. Can someone let me know what I'm missing?

import java.util.ArrayList;
import java.util.Scanner; // program uses class Scanner

class EmployeeData { // start of EmployeeData class

			EmployeeData(String newName, float newHourly, float newHours) {
			name = newName;  hourly = newHourly;  hours = newHours;
			}

public String getName() { return name; }

public float getProduct() { return hourly * hours; }

private String name;

private float hourly, hours;
		
		} // end of EmployeeData class

public class Payroll4

{ // start of public class

	private String EmployeeData; // employee information for this Payroll

	private static int ArrayList;

	private static int i;

		// main method begins execution of java application
		public static void main( String args[] )
			{ // Start main method

				ArrayList employees = new ArrayList (); 
				
					// create scanner to obtain input from command window
					Scanner input = new Scanner ( System.in );
						
						System.out.println();
						System.out.print( "Enter employee name or stop to quit: " ); 					                        String empName = input.nextLine(); // read employee name
						
						while (empName.equalsIgnoreCase("STOP") != true)

							{ 
								
								EmployeeData employee;
								float hourly; // Employee Hourly Rate
								float hours; // Employee Hours Worked
								float product; // Employee Salary for Week
					
								{ 
									System.out.print( "Enter hourly rate: " ); // prompt
									hourly = input.nextFloat(); // read hourly rate from user

										while (hourly <= 0) 
											{ 
												System.out.print( "Please Enter postive hourly rate: "); // prompt for postive hourly rate
												hourly = input.nextFloat(); // read hourly rate from user
											} 

									System.out.print( "Enter hours worked for week: "); // prompt for hours
									hours = input.nextFloat(); //read number of hours worked

										while (hours <= 0) 
											{ 
												System.out.print( "Please enter postive hours worked: "); // prompt for postive hours worked
												hours = input.nextFloat(); // read number of hours worked
											} 

								employee = new EmployeeData(empName, hourly, hours) {
			
								 													};

								System.out.println();
								System.out.print( employee.getName( ) ); // display employee name
								System.out.printf( "'s Weekly pay: $%,.2f\n", employee.getProduct() );  // Display weekly pay
								System.out.println();
								
								System.out.println();
								System.out.print( "Enter employee name or stop to quit: " ); // prompt for employee name
								String empName = input.nextLine(); // read employee name
							} 
					
					System.out.println();
					System.out.println( "Thank you, Goodbye" );

	} // end main method

}} // end class Payroll4
(empName.equalsIgnoreCase("STOP") != true)

should read:

(empName.equalsIgnoreCase != stop)

Hi jcato77 and welcome to DaniWeb,

jamesbien is completely wrong with his advice. But I am not sure about your question. Do you mean that the code inside the loop that jamesbien referenced never executes, or that it executes even when the user inputs the word "STOP"?

Hello darkagn

I am able to enter the first Name and Hours worked and payrate, when the loop continues it does not ask for empName again. If I place the same empName request at the end of my program it does not complie. I'm told empName has already been defined

Ahhhh

Ok, your line at the bottom of your code that reads

String empName = input.nextLine(); // read employee name

remove the word "String" from this line.

The problem here is that you have already defined empName but you want to assign a new value to it. When you say

String empName = input.nextLine();

you are defining the variable but also assigning an initial value to it. After that to reassign a new value, you just write

empName = input.nextLine(); // get another line of input

Then when the loop comes back to the top it will check the same variable with the new value.

Thank you for the advice, I was able to fix it off of your comments and a little more research through my books.

This is my first post, and I am a java noob. I had a similar assignment to complete this weekend. After reading this post I solved the problem. The while loops condition has to be met, and it has to have an input to tell it not to loop again. I hope that makes sense, but here is what I have for my code.

package pay_1;

import java.util.Scanner;

public class Calcu {
	

	// Program execution starts here
	public static void main ( String [] args ) 
	{ 
		// Start Scanner
		Scanner input = new Scanner (System.in);
		String name;
		String name2;
		double payRate;
		double hoursWorked;
		
		
		 
		
		// Begin User Input
		System.out.println("Welcome:-)\nEnter employee's first name\nor type stop to quit");
	    name = input.next();
	    
	    // loop until stop begins
	    while ( !name.equals("stop") )
	    {
	    	  
		        	 System.out.println("Enter employee's last name");
		        	 name2 = input.next();// read last name from user
		        	 
		        	 System.out.println("Enter Pay Rate\n$");
		             payRate = input.nextFloat(); // read hourly rate from user
		             
		             // start pay rate while loop
		             while (payRate <= 0) 
		             { 
		            	 System.out.print( "Please use positive numbers: "); // prompt for input of positive number
		            	 payRate = input.nextFloat(); // read hourly rate from user
		             } 
		             
		             System.out.println("Enter number of hours worked");	    
		             hoursWorked = input.nextFloat(); // read hourly rate from user
		             
		             // start hours worked loop for positive number
		             while (hoursWorked <= 0) 
		             { 
		            	 System.out.print("please use positive numbers"); // prompt for positive hourly rate
		            	 hoursWorked = input.nextFloat(); //  user input
		             } 				
				
		             // Calculations Begin
		             Object product = payRate * hoursWorked;
		
		             //Display Results
		             System.out.println(name);//print first name
		             System.out.println(name2);//print last name
		             System.out.printf("total income this week is $%.2f\n", product);
		             
		             //1st prompt for user input in loop
		             System.out.println("Enter employee's first name\nor type stop to quit");//
		             name = input.next();
	   
		
	   }//end while
	                 // goodbye message
	                 System.out.println("Thank you for using "your name here"'s payroll program.");
	 }// End main method 
	

}// End pay_1

I noticed that many of the similar assignments on the net are a little more advanced than mine is, so I will probably post solutions that I . This one works its not perfect, but I hope it helps you find the answer to your problem. Finally thank you to the Dani Web community for helping me solve this and many other problems.

This question has already been answered. Start a new discussion instead.