Results 1 to 5 of 5
  1. #1
    antdilla22 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default Help getting my program to continue requesting info.

    I am new to java, i taking online courses and i am having trouble with one of my assignments. All I have to do is get the program to continue to request employee information until the user enters stop as the employee name. If anybody could help I would be greatly appreciative.

    Java Code:
    // fig. 1: PayrollProgram.java
    // A payroll program that calculates weekly pay for an employee
    import java.util.Scanner; // program uses class Scanner 
    
    public class PayrollProgram
    {
        // main method begins execution of Java application
        public static void main( String [] args )
        {
    	Scanner input = new Scanner( System.in );
    	
    	int hoursWorked;
    	int hourlyRate;
    	int grossPay;
    
    	System.out.print ( "Enter employee name: " ); // prompt for employee name
    	String theName = input.nextLine();
    
    	System.out.print ( "Enter hours worked: " ); // prompt for hours worked
    	hoursWorked = input.nextInt();
    	if(hoursWorked < 0)
    	    System.out.println(" number must be positive ");
    
    	while (hoursWorked < 0)
    	{
    	    System.out.print ( "Enter hours worked: " );
    	    hoursWorked = input.nextInt();
    	    if(hoursWorked < 0)
    		System.out.println("number must be positive");
    	} // end while
    
    	System.out.print ( "Enter hourly rate: " ); // prompt for hourly rate
    	hourlyRate = input.nextInt();
    	if(hourlyRate < 0)
    	    System.out.println("number must be positive");
    
    	while (hourlyRate < 0)
    	{
    	    System.out.print ( "Enter hourly rate: " );
    	    hourlyRate = input.nextInt();
    	    if(hourlyRate < 0)
    		System.out.println("number must be positive");
    	} // end while
    
    	// multiply hours worked * hourly rate
            grossPay = hoursWorked * hourlyRate;
            // display employee name and weekly pay total
            System.out.printf( "Employee Name: %s,  Gross pay is $%d%n",
                                theName,  grossPay );
    	
    	} // end method main
    
    } // end class PayrollProgram
    Last edited by Fubarable; 09-13-2009 at 10:41 PM. Reason: Code tags added for readability

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    The whole chunk of code that accepts input from the user must be enclosed inside a while loop which should break when the name entered becomes "stop".
    P.S Use code tags next time when posting code.

  3. #3
    antdilla22 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default I kind of understand

    I understand that there needs to be a while loop but I just cant figure out where to write it and the proper wording. I have tried several different ways, actually i have been working on it for the past day and a half, and it wont compile. Like I said earlier, I am new to this so it is confusing to me. I am using a notepad and command prompt to do this. Also, what are code tags? Thank you for your time.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Post what you have now then with the while loop for the stop check.

Similar Threads

  1. Replies: 1
    Last Post: 10-20-2008, 12:58 PM
  2. Program doesn't continue...
    By Reiyn in forum New To Java
    Replies: 7
    Last Post: 10-07-2008, 10:28 PM
  3. Replies: 1
    Last Post: 06-23-2008, 06:46 AM
  4. press any key to continue
    By dotnet007 in forum New To Java
    Replies: 3
    Last Post: 05-11-2008, 05:19 AM
  5. How to use Continue
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •