User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 2
Reputation: dno117 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dno117 dno117 is offline Offline
Newbie Poster

Java rookie needs help with sentinel while loop

  #1  
Nov 17th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: Java rookie needs help with sentinel while loop

  #2  
Nov 17th, 2007
You don't compare Strings with either == or != use
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
Reply With Quote  
Join Date: Nov 2007
Posts: 2
Reputation: dno117 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dno117 dno117 is offline Offline
Newbie Poster

Re: Java rookie needs help with sentinel while loop

  #3  
Nov 17th, 2007
Thank you Masijade, your input has been very helpfull and I greatly appreciate it



dno117
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:04 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC