I am now on part 3 of a payroll program for my Java class but I did not add the while loop in last week so I need to figureout what I did wrong so I can do part 3.

The program should loop until the user enters "stop" It sort of does it but after the program is complete it asks for pay rate again, not employee name. I see why but I cannot figure out how to fix it. I found this forum and I am hoping someone can help me.
This is what I have:

package payroll4;

/**
 *
 * @author Brianna
 */
import java.util.Scanner;

public class Payroll4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        {
        Scanner input = new Scanner( System.in);

        System.out.println();
        System.out.print( "Enter Employee's Name or Stop if you would like to exit: ");
        String empName = input.nextLine();
    while(!(empName.equals("stop")))    
    {   

              double hourlyRate; //rate per hour
              double hoursWorked; //hours worked
              double weeklyPay;  //hours*rate

            System.out.print( "Enter the rate per hour: ");  //prompt for hourly rate
            hourlyRate = input.nextDouble(); //read hourly rate

    while (hourlyRate <= 0) //prompt until the number is positive
    {
                System.out.print( "Rate per hour must be a positive number." +
                        "Please re-enter the rate per hour: " );
                hourlyRate = input.nextDouble();  //read rate per hour again
            }

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

    while (hoursWorked <= 0) //prompt until the number is positive
    {
                        System.out.print( "Number of hours worked must be a positive " +
                                "number. Please re-enter the number of hours worked: " ); //prompt for number of hours worked
                        hoursWorked = input.nextDouble(); //read hours worked again
    empName =input.next();
    }//end while

                    {
                   //calculation of weekly pay.

                    weeklyPay =(float) hourlyRate*hoursWorked; // *Sum
                    }
                    {
                    //Display of out
                    System.out.print( empName ); //display the employee name
                    System.out.printf( "'s pay this week is: $%,.2f\n",weeklyPay);
        }
        }
    }
} //end method main
}// end class Payroll4

Recommended Answers

All 12 Replies

I wanted to say too that I am not looking for just the answer, I really want to understand this and have been stuck for a few days. Thanks again

You get the name before you enter the loop, so that's only executed once, at the start of the program. Your main two options are:
1. repeat lines 21/22 just before the end of the loop, so you ask for "name or stop" before starting the next pass of the loop
2. use a different loop structure, eg

while (true) {
   prompt for "name or stop"
   if (input is "stop") break;
   ask for pay etc...

ps: You ask the user to input "Stop", but test for "stop". There's a version of equals that ignores case - see the String API doc for details.

Thank for your suggestion, I had tried repeating line 21 and 22 but then just the name part loops. The entire program should loop back to name. I asked my instructor this question and she said "move the while loops for pay rate and hours into this outer while loop for name" I still haven't been successfull

Yes. Your instructor has given you good advice.
It's not easy to get nested loops right at first. The simplest and most useful thing is to ensure your indentation is always right, so you can see what's in which loop. If I was your instructor I would refuse to waste my time lookng at code that wasn't correctly indented, because it's so hard to understand.

It should look something like like this:

while loop for name or stop{
   ...
   while loop for pay rate {
      ...
   }
   while loop for hours {
      ...
   }
   calculate and print results
} 

I don't know why I am having such a hard time with this. Thank you for your advice. Is there any way you could put those loops in the proper place in the code that I posted? I think I need to actually see it to understand. I have read my text book, watched Youtube vids, searched the internet and I have a block on this for some reason. I have been fine until this point. This seems like such a simple concept but I am at a loss and have to modify it again for the next assignment by today.

I could fix your code. But if you handed that in you would be cheating and get an automatic fail. (Remember that your instructor may read this site as well!)
I'm happy to try to help you do this yourself, but that's all.

Try what I said - indent the code properly and follow the template I gave you. That should help you get all your loops in the right place.

I have to go soon, but hopfully someone else will be able to step in if you need more guidance.

I don't think it would cheating to show me an actual example and I would hope my instructor reads this site. I see that many people from my school use this :P I spent so much time on last weeks assignment and I see that it is posted here many times. I guess this forum could be abused. Thanks for your help. I may just give up on this. I am at a block and can't seem to get past it.

Please don't give up. Programming isn't easy, but it is logical, and you will get there if you work at it in a logical organised way.

Its not easy, but I thought would do ok. I am just a visual person and reviewing books is just not helping me. I think my error is a simple one but I typed it over and still have the issue. I think I am having a hard time getting where each piece goes. Thanks though!

This is what I started changing it to and it says my I have an empty statement after while

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println();
        System.out.print("Enter Employee's Name or stop to exit");
        String empName = input.nextLine();
    while(!(empName.equals("stop")));
    {  
        double hourlyRate;

        System.out.println("Enter rate per hour:");
        hourlyRate=input.nextDouble();

        while (hourlyRate <=0){
            System.out.print("Rate must be positive, please try again");
            hourlyRate = input.nextDouble();
        }

        double hoursWorked;

        System.out.println("please enter hours worked");
        hoursWorked = input.nextDouble();

        while (hoursWorked <=0){
            System.out.println("hours worked must be positive, please try again");
            hoursWorked = input.nextDouble();
        }


    }

I found my mistake! OMG I feel dumb. Thanks for all your help! Now onto todays assignment. WHEW!

you'll need to get used to reading, as there's a lot of things to know and just watching videos on youtube or watching your teacher demo things isn't going to teach you much of anything...

Read, experiment, analyse, rince and repeat. That's the only way. And ask questions, not just here but ask your teachers. Discuss things with them and your classmates as well.

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.