Hello all,

I am an online student in an intro to Java programming course and am a little stuck on my current program. I do not want the answer, but rather help figuring out what it is I am doing wrong. The program is supposed to prompt for an employee name, their hourly rate of pay, the number of hours they worked, and output their weekly pay. I have that part down, but now the course asks us to expand on that program to include a loop for the employee name, hourly rate, hours worked, and total weekly pay. The loop should be terminated when stop is entered for the employee name. I haven't got to the "stop" part yet because I cannot get the program to loop correctly. In command prompt, this is what it looks like...

Enter employee name or stop to quit:  John Doe
Enter rate of pay: 5.00
Enter hours worked: 10
Total weekly pay for John Doe is $50.00
Enter employee name or stop to quit: Enter rate or pay:

My code is not complete, as I haven't got past the loop. Here is my code

//Checkpoint Payroll Program Part 2
//Multiplication program that calculates and displays an employees weekly pay

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

public class Payroll2

    //main method begins execution of Java application
    public static void main( String args[] )
        // create Scanner to obtain input from command window
        Scanner input = new Scanner( System.in );

        String fName; // employee name
        float rate;// first number to multiply
        float hours;// second number to multiply
        float total;// total of rate times hours
        float counter;// number of employee names entered

        // processing phase
        // prompt for input and read employee name from user

        System.out.print( "Enter employee name or stop to quit: " );// prompt
        fName = input.nextLine(); // read employees name

        // loop until sentinel value read from user     
        while ( fName != "stop" )

            System.out.print( "Enter pay rate: ");// prompt
            rate = input.nextFloat(); // read first number from user

            System.out.print( "Enter hours worked: ");// prompt
            hours = input.nextFloat(); // read second number from user

            total = rate * hours; // multiply numbers

System.out.printf( "Weekly pay for " +fName+ " is $%   s\n", total ); //display employee name and weekly total

        } // end while

      }  // end method main

 }  // end class Payroll2

Any help would be GREATLY appreciated. Also any tips for setting the sentinel value would be great, as I haven't been able to find in my text if I
1) Need to set the sentinel value
2) How to set the sentinel value

Thanks much,


Recommended Answers

All 2 Replies

You don't compare Strings with either == or != use

while(!"stop".equals(fName)) {

And, before you ask, putting "stop" instead of fName first, avoids a possible NullPointerException if fName happens to be null. That does not mean you won't have problems inside the while if it is null, so you should probably do

while ((fName != null) && (!"stop".equals(fName)) {

Thank you Masijade, your input has been very helpfull and I greatly appreciate it


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.