I have been trying for two days to figure out how to use a loop in my program. It is the ever dreaded payroll program. I can't seem to get the program to start over once the program goes through each question. Can anyone point me in the right direction (other than use a loop...I can't figure out how to get a loop!) I will really appreciate any help!

import java.util.Scanner; // program uses class Scanner
public class PayrollProgram {

    /** Creates a new instance of PayrollProgram */
    public PayrollProgram() {
    }

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

        int number1; // hourly rate
        int number2; // hours
        int product; // product of number1 and number2


        System.out.println("Please enter your name or stop to quit the program:");//prompt
        String nameOfEmployee=input.nextLine();  

        while(nameOfEmployee !="stop"){

        }


       {
            System.out.println("Ending program");
        } 


        System.out.println("Enter hourly rate:$");//prompt
        number1 = input.nextInt(); // read first number
        while (number1 <=0)//prompt until user enters positive value
        {
           System.out.println ("Hourly rate must be positive. Please enter hourly rate again");//prompt user to enter in hourly rate again
           number1 = input.nextInt(); // read first number
        }


        System.out.println( "Enter hours: " ); // prompt
        number2 = input.nextInt();//read second number
        while (number2 <=0)//prompt until user enters positive value
        {
            System.out.println ("Hours must be positive. Please enter hours worked again");//prompt user to enter in hours worked again
            number2 = input.nextInt();//read second number
        }

        product = number1 * number2; // multiply numbers
        System.out.print( nameOfEmployee ); // display employee name
        System.out.printf("'s weekly pay is $%d\n",product); 



      }//end main method

     }//end class PayrollProgram

you actually already have the loop defined, you just forgot to put the code you want to be running during this loop

while(nameOfEmployee != "stop"){

}

between the brackets of this loop.
if you want for every employee you enter to give the numbers and print the outcome, you will need to put this code between the brackets of the above loop.

at the end of this loop, you will need to ask the user to enter a new userName, otherwise you will never be able to reach the end by entering "stop", it 'll just continue to print the data for the first user entered.

what you certainly may not forget, is that in this loop, you are checking wether a String-Object contains a certain value, so the above code, as used in your program, is not correct. Objects must always be compared using the equals(); method.

fi:

while(!nameOfEmployee.equals("stop")){
// enter the code you want to run for every employee here
}

Thank you so much!!! I can't tell you how much plaster you have saved me from buying with me banging my head on the wall.....

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.