Results 1 to 5 of 5
- 09-13-2009, 07:57 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
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 11:41 PM. Reason: Code tags added for readability
- 09-13-2009, 10:00 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 13
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.
- 09-13-2009, 11:40 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 2
- Rep Power
- 0
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.
-
For while loops, please look here: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
As for code tags, I added them to your first post. Please have a look at my signature below in blue.
- 09-13-2009, 11:44 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 13
Similar Threads
-
Requesting some tips to implement an interface between a tree structure and table
By Karanam in forum AWT / SwingReplies: 1Last Post: 10-20-2008, 01:58 PM -
Program doesn't continue...
By Reiyn in forum New To JavaReplies: 7Last Post: 10-07-2008, 11:28 PM -
Requesting help in copy paste of folder similar to windows.
By selvin_raj in forum Advanced JavaReplies: 1Last Post: 06-23-2008, 07:46 AM -
press any key to continue
By dotnet007 in forum New To JavaReplies: 3Last Post: 05-11-2008, 06:19 AM -
How to use Continue
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 08:46 PM
Bookmarks