Hi,

Can anyone tell me why this program is skipping line 63?

Thank you,
lynnajoe

import java.util.Scanner;   		//Imported Scanner for input
import java.util.Locale;  		//Imported Locale and NumberFormat
import java.text.NumberFormat;


/**Simple Payroll Program titled PayrollPartI & modified for PayrollPart II
 *
 * @author Lynn Ortiz
 *///May 22, 2011   //Description of program:  This program tabulates the base
                   //salary and overtime pay for employees
		   //It will exit if "Stop" is entered for an employee name


public class PayrollPartIandPartII {//This is the class for PayrollPartI
                                    //modified for PayrollPartII
private static String name;
                                         //Declared variables
private static float hourlyRate;   	 //Employee
private static float hoursWorked;
private static float regHours = 40;
private static double employeeBaseSalary = hourlyRate * regHours;
private static NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
private static boolean isName = true;
private static float overtimeHours;
private static float overtimePay;
private static double employeeWeeklySalary;

public static void main (String []args){ //Beginning of the main method

    System.out.println("Welcome to the Employee Payroll Program\n");//Welcome screen
   						//Blank line
    System.out.println("Enter Stop for employee name to exit program\n");//Instructions to exit program
 while (isName){
    Scanner input = new Scanner(System.in);//The while loop until "Stop" is entered

    System.out.print("Please enter employee name: " ); 	//Prompts for user input
    name = input.nextLine();

         if (!name.equalsIgnoreCase("stop")){
         System.out.print("Please enter employee's hourly rate: ");	 //Continue loop
         hourlyRate = input.nextFloat();

         if (hourlyRate < 0){				//If a negative number is input for employee hours or rate and error message prompts
        System.out.print("please enter a positive number: ");	//user to input a positive number
         hourlyRate = input.nextFloat();
         }
    System.out.print("Please enter employee hours worked:  ");
    hoursWorked = input.nextFloat();              //Variables for holding values

         if (hoursWorked < 0){				//Prompts for user input
        System.out.print("Please enter a positive number:  ");
        hoursWorked = input.nextFloat();
        }
         if (hoursWorked > 40){
         overtimeHours=hoursWorked - regHours;
                 
                 employeeBaseSalary = hourlyRate * regHours;		//Calculates base salary
    System.out.println("Employee base salary is:  "+nf.format(employeeBaseSalary)); //Format currency
    System.out.print("Overtime hours:  " + overtimeHours);
    overtimePay = (float) (overtimeHours * 1.5 * hourlyRate);//Calculates overtime pay
    System.out.println("\nEmployee's overtime pay:  "+nf.format( overtimePay));
    employeeWeeklySalary =( employeeBaseSalary + overtimePay);
    System.out.println("Employee weekly salary:  "+nf.format(employeeWeeklySalary));
       
         
       			//This ends the while loop
       if( isName =false){
        System.out.println("Thank you for using the Employee Payroll Program");
                                        //Exit message
     }
 }
    }}}}


 
					//End of main method

Here's one problem:

if (!name.equalsIgnoreCase("stop")){

What happens if this branch doesn't fire? What happens if I enter "stop"?

Now, I don't know which is the line 63 that you mean - I can't see any reason why the line numbered 63 here:

System.out.println("Employee weekly salary:  "+nf.format(employeeWeeklySalary));

wouldn't execute. But I do see a reason why we never get to here:

if( isName =false){
System.out.println("Thank you for using the Employee Payroll Program");

and that's because you don't ever seem to set isName to false.

Hope this helps.

Hi,
Thank you for trying to help me. If I type in stop the program performs the way it is supposed to. I am not sure I even need that line. It skips over line 63 the line where it is supposed to print the weekly salary. It skips over and prompts for the employee name again. I will keep at it until I iron out that wrinkle. I keep refining it until I can use the proper syntax.

Thanks again,

lynnajoe

What gets executed if hours worked are >0 but < 40?

You keep nesting if within if within if until by the end (eg line 63) there's only a small chance of the code being executed. The presence of six consecutive } at the end, although perfectly legal, is also a warning sign that the logic may be bad.

Fix your indentation and you'll be able to see this clearly

Member Avatar for kris0r

It works fine for me, cleaned it up a bit so I personally found it easier to read. And I get

Welcome to the Employee Payroll Program

Enter Stop for employee name to exit program

Please enter employee name: chris
Please enter employee's hourly rate: 6.50
Please enter employee hours worked:  80
Employee base salary is:  $260.00
Overtime hours:  40.0
Employee's overtime pay:  $390.00
Employee weekly salary:  $650.00

slightly edited program:

import java.util.Scanner;
import java.util.Locale;
import java.text.NumberFormat;

public class PayrollPartIandPartII 
{
	private static String name;
                                       
	private static float hourlyRate;
	private static float hoursWorked;
	private static float regHours = 40;
	private static double employeeBaseSalary = hourlyRate * regHours;
	private static NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
	private static boolean isName = true;
	private static float overtimeHours;
	private static float overtimePay;
	private static double employeeWeeklySalary;
	
	public static void main (String []args)
	{
		System.out.println("Welcome to the Employee Payroll Program\n");
		System.out.println("Enter Stop for employee name to exit program\n");
		
		while(isName)
		{
			Scanner input = new Scanner(System.in);
	
			System.out.print("Please enter employee name: " );
			name = input.nextLine();
	
			if(!name.equalsIgnoreCase("stop"))
			{ 
				System.out.print("Please enter employee's hourly rate: ");
				hourlyRate = input.nextFloat();
	
				if(hourlyRate < 0)
				{
					System.out.print("please enter a positive number: ");
					hourlyRate = input.nextFloat();
				}
			
				System.out.print("Please enter employee hours worked: ");
				hoursWorked = input.nextFloat();
	
				if (hoursWorked < 0)
				{
					System.out.print("Please enter a positive number: ");
					hoursWorked = input.nextFloat();
				}
				
				if(hoursWorked > 40)
				{
					overtimeHours=hoursWorked - regHours;
					employeeBaseSalary = hourlyRate * regHours;
		
					System.out.println("Employee base salary is:  "+nf.format(employeeBaseSalary));
					System.out.print("Overtime hours:  " + overtimeHours);
					
					overtimePay = (float) (overtimeHours * 1.5 * hourlyRate);
					System.out.println("\nEmployee's overtime pay:  "+nf.format( overtimePay));
					
					employeeWeeklySalary =( employeeBaseSalary + overtimePay);
					System.out.println("Employee weekly salary:  "+nf.format(employeeWeeklySalary));
		
					if(isName = false) System.out.println("Thank you for using the Employee Payroll Program");
				}
			}
		}
	}
}

Look at line 65 in your code. The condition contains an assignment statement.
Is that what you want?

Hi kris0r.
Can you run that again with an hours worked of 20 please?
Thanks

Hi,

I have actually come across the very problem you are speaking about. Can someone point me in the right direction instead of all those nested "ifs"? I really do not want to be a sloppy programmer but sometimes the syntax seems backwards to me. Is there somewhere or someone that can just give a little bit of aide as to what I can use to replace all of those statements? Any suggestions are well appreciated and I am really trying to learn this language.

Thank you,

lynnajoe

What is the purpose of the outer if in the while loop? To skip ALL of the code in the while loop.
Instead, you could break out of the while loop when the exit condition is found.

What happens with your code if you enter "stop"? What is supposed to happen?
Where do you tell the user he can enter stop?

There's really no alternative to a lot of ifs when there a lot of different things that can happen based on user input. You can use a switch statement in some cases, but it probably won't help in this particular program.
I suspect that at least some of your problem is because you are nesting ifs when they shouldn't be nested. Good indentation is essential for understanding this kind of code - just look at kris0r's version and how much easier it is to understand.

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.