•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,964 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,630 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2628 | Replies: 2 | Solved
![]() |
•
•
Join Date: Nov 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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,
dno117
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,
dno117
You don't compare Strings with either == or != use
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(!"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)) { Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- How is colon : used in for{} loop? (Java)
- what is this for loop (Java)
- Need help,my C++ calc (C++)
- NoClassDefFoundError (Java)
- Help with investment calculator (Java)
- Send all items in a listBox to SQL server database table (ASP.NET)
- Source Code Blues (Java)
- help me understand end of do while loop (C++)
- Loop...without the loop (Java)
Other Threads in the Java Forum
- Previous Thread: help the othello
- Next Thread: Stack and queue



Linear Mode