0

Modify the Payroll Program application so it continues to request division information until the user enters stop as the division name. In addition, program the application to check that the number of employees and average salary per employee are positive numbers. If either the number of employees or the average salary per employee is not a positive value, the application should prompt the user to enter a positive amount.

This is the code I have come up with, which compiles fine but doesn't run the way I expect it to.

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

public class PayrollPart2
{

    public static void main( String[] args ) 
    {
        Scanner input = new Scanner( System.in ); // create Scanner to obtain input from command window

        // variables
        char name; // divisions's name
        int number1; // number of employees in the division
        double number2; // average salary for the employees
        double product; // total division payroll

            //prompt user to input division name
            System.out.print( "Enter Division's name, type stop to exit: ");
            String divisionName = input.nextLine(); //read line of text
                while (divisionName !="stop") 
                {
                //prompt user for number of employees in the division
                System.out.print( "Enter the number of employees in the division: ");
                //read number of employees from user's input
                number1 = input.nextInt();
                    while (number1 <= 0)
                    {
                        System.out.print("Please enter a positive number of employees in the division:"); // prompt
                        number1 = input.nextInt(); // input
                    }

                //prompt user to enter average salary of employees
                System.out.print("Enter average salary for the employees: " );
                //read average salary
                number2 = input.nextDouble();
                    while (number2 <= 0)
                    {
                        System.out.print("Please enter a positive number for the employees salary:"); // prompt
                        number2 = input.nextDouble(); // input
                    }

                //multiply Number1 by Number2
                product = number1 * number2;

                //displays division and total division payroll
                System.out.printf( "The division %s has a payroll of $%.2f\n" , divisionName, product );
                }
    } //end method main
} // end class PayrollPart2

I've been at it for about 8 hours and at this point I am completely lost on the next steps. The code goes through the loop but doesn't ask for a different division name. Typing the exit command on the first prompt doesn't actually exit the loop. Should I be using If/Else statements instead? I think I can use a Boolean but I'm not exactly sure how to implement it.

Bob B.
  • 25
  • 1
  • 1
  • 2
  • 1
    Also, `divisionName` never changes in the loop. So it's either skipped completely or an infinite loop. – jlordo Aug 17 '13 at 01:02
  • Well, I wouldn't call it a duplicate; the poster isn't asking explicitly about comparing `String`s. – Dennis Meng Aug 17 '13 at 01:03
  • 1
    @DennisMeng - Closing a question as a duplicate is not necessarily a reflection on the Question or the OP. The point is to get rid of Questions that won't help other people in the future. And this Question won't. IMO, this is a genuine well-asked (+1) Question ... but it is also yet another example of someone comparing strings the wrong way, and therefore a duplicate. – Stephen C Aug 17 '13 at 01:19
  • Fair enough. Guess I should read more on question closing. – Dennis Meng Aug 17 '13 at 01:20

1 Answers1

5

Use string equals() method to do the string comparison instead of != or ==. One of the problem in your code is this statement:

while (divisionName !="stop") 

change it to

while(!("stop".equals(divisionName)))

!= will chech whether the two objects are not the same memory object, while equals method will do the string contents comparison.

Also note the reverse comparision of string "stop" against the divisionName. This will help to avoid null pointer exception in case divisionName is null.

Learn more about string comparison from a related post: Java String.equals versus ==

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • As an aside, I doubt that `nextLine()` will ever return `null`. Also, if the string should never be `null`, there's something to be said for not doing the reverse comparison. That way, if it is ever `null`, you know about it immediately and can debug the root cause, rather than letting it persist through. – Dennis Meng Aug 17 '13 at 01:08
  • `!"stop".equals(divisionName)` So weird to see it like that... Variable first. Come on man. ;) –  Aug 17 '13 at 01:08
  • 3
    @DummyCode Putting the constant first allows it to handle the case where `divisionName` is `null` without a separate check or a NPE. – Jason C Aug 17 '13 at 01:17
  • Changing the string comparison worked for exiting the loop, now I guess my question is how do I get the loop to ask for another division name. Any changes I've made won't compile. – Bob B. Aug 17 '13 at 01:21