Results 1 to 14 of 14
  1. #1
    jsand2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default Payroll Program exits at wrong time

    Hello everybody!

    I am currently taking a Java class at school that requires me to make java programs. I for the most part have my program complete and can run it without errors, but there is one error (or at least I call it that) that I want to fix. When I type "stop" into the employees name prompt it still runs the rest of the program before exiting. I was wondering where I misplaced that part of the code. I want it to actually terminate program upon me pressing "enter" after typing "stop". Thanks in advance for your response!

    Here is my code:

    Java Code:
    // Week 3 - Day 5 - Payroll Program Part 2 
    // John Sanders 
    // IT 215
    
    import java.util.Scanner; // class scanner
    
    public class Payroll2 // set public class
    
    { 
    	
    	public static void main( String args[] )
    	
    	{
    		Scanner input = new Scanner( System.in );
    		
    		String cleanInputBuffer; // input
    		String empName; // input employee name
    		double hourlyRate; // input hourly rate
    		double hoursWorked; //input hours worked
    		double weeklyPay; // weekly pay amount
    		boolean end = false; // is the input name stop?
    		while( end == false ) // as long as end is false, continue
    		
    		{
    			
    			System.out.print( "Enter Employee's Name:" ); // prompt to enter employee name
    			empName = input.nextLine(); // input employee name
    			if( empName.toLowerCase().equals( "stop" )) // if employee name = stop
    				end = true;  // when stop is detected, change the boolean, which ends the while loop
    		
    		while( hourlyRate < 0 ) // while the hourly rate is < 0
    		
    		{
    				
    			System.out.print( "Enter a positive hourly rate:" ); // print enter a positive hourly rate
    				hourlyRate = input.nextDouble();
    				
    		}
    		
    		while( hoursWorked < 0 ) // while the hours worked are < 0
    		
    		{
    			System.out.print( "Enter a positive number of hours worked:" ); // print enter a positive number of hours worked
    			hoursWorked = input.nextDouble();
    			
    		}
    		
    		weeklyPay = hourlyRate * hoursWorked; // multiply hourly rate by hours worked for weekly pay
    		
    		System.out.printf( "The employee %s was paid $ %.2f this week", empName, weeklyPay ); // print final line
    		System.out.println();
    		
    		cleanInputBuffer = input.nextLine();
    		
    		} // end outer while
    		
    	} // end main method
    	
    } // end class Payroll2

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Here in this line,

    Java Code:
    end = true;
    you haven't set the exit the code. Just set a value. So it's execute the rest of the code, and in next iteration cause to exit your application.

  3. #3
    jsand2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    I am sorry but I am quite new to this and feel pretty lost. I do not exactly know what you mean by that. I have tried many different things, nothing changing what is going on. I have been trying to research the internet, my schools reading, and even a Java Programming for Dummies book. I can not find anywhere what I am supposed to do with this. You would think my schooling would have provided an example that would help, but none of the examples deal with exiting a program. If you could clear things up a little with what you mean that would be great, sorry for my ignorance.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Here we go...

    The while loop won't evaluate the value of the empName until it starts the next loop. If you want break out of the while loop at that point, you can use the "break" command.

    Branching Statements (The Javaô Tutorials > Learning the Java Language > Language Basics)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Or else you can use the return statement like mentioned below.

    Java Code:
    if( empName.toLowerCase().equals( "stop" ))
           return;
    And also when I'm going through your code I found two errors on inner while loops. Seems that you have use two variables without initialization. It's not good practice, and also you cannot do that with local variables. You must comes with an error when you run this code. Didn't you get any?

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default break vs return

    Just a clarification for the OP's knowledge...
    • A "break" command will break out of a loop's execution, but will continue with the code execution after the loop.
    • A "return" statement will exit the current method execution.


    In this case, with the OP's code, both have the same effect.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by CJSLMAN View Post
    • A "break" command will break out of a loop's execution, but will continue with the code execution after the loop.
    • A "return" statement will exit the current method execution.
    and System.exit() will terminate the whole program. Again, in this case, this will have the same effect as "break" or "return" used instead of "end=true"
    Last edited by OrangeDog; 01-25-2009 at 05:55 AM. Reason: clarification

  8. #8
    jsand2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post

    And also when I'm going through your code I found two errors on inner while loops. Seems that you have use two variables without initialization. It's not good practice, and also you cannot do that with local variables. You must comes with an error when you run this code. Didn't you get any?
    No I dont receive any errors what so ever. I just assumed it was all fine because of that.The only time I receive an error is when I leave a prompt blank and then just press enter. Outside of that it seems to run fine on my end.

  9. #9
    jsand2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    [COLOR="DarkGreen"]Or else you can use the return statement like mentioned below.

    Java Code:
    if( empName.toLowerCase().equals( "stop" ))
           return;
    So I have tried this as well as well as break and system.exit() and all compile with no errors. However none seem to exit the program until it fully runs. I do not understand what I am missing. I ended up turning this assignment in yesterday after messing with this for 6 hours and just taking a hit on the grade. I am still curious however to see what it takes to make this work.

    You mentioned that I had errors with my variables. Could that be what is making this program screw up still? I just dont understand what else would be wrong. I mean you actually typed the code out that I needed to add but it still fails so I am wondering what other issues could cause this.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Comments...

    Java Code:
    if( empName.toLowerCase().equals( "stop" ))
           return;
    If you used the above code, it will exit. Now, it depends where you put that code. It depends also on how you have change your program (would be a good idea to post your code again, since it's probably changed). Did you put it as the first statement in the first while loop? This would also probably change the while statement to
    Java Code:
    while(true) {....
    Now ... about the two errors that Eranga found...
    Java Code:
    while( hourlyRate [B][COLOR="Red"]<[/COLOR][/B] 0 )
    ...
    while( hoursWorked [B][COLOR="red"]<[/COLOR][/B] 0 )
    What the above means is that the "while" loops will execute while hourlyRate and hoursWorked are less than zero !!!
    I think that you wanted to code the following:
    Java Code:
    while( hourlyRate [B][COLOR="Blue"]>[/COLOR][/B] 0 )
    ...
    while( hoursWorked [B][COLOR="blue"]>[/COLOR][/B] 0 )
    Did this help any?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    jsand2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    CJSLMAN,

    I took your advice and changed the stuff you presented. It still does not exit the program immediately, instead it is still finishing the program first. I am going to go ahead and repost my work with all of the fixes that you presented that way you can see where I stand with the corrections that you provided. I find it very weird that it is still not working. I have been posting here and on DaniWeb and pretty much getting the same answers but for some reason they dont want to work in my program. I am sure that it is just me, but am too lost to know the truth.

    Java Code:
    // Week 3 - Day 5 - Payroll Program Part 2 
    // John Sanders 
    // IT 215
    
    import java.util.Scanner; // class scanner
    
    public class Payroll2 // set public class
    
    { 
    	
    	public static void main( String args[] )
    	
    	{
    		Scanner input = new Scanner( System.in );
    		
    		String cleanInputBuffer; // input
    		String empName; // input employee name
    		double hourlyRate; // input hourly rate
    		double hoursWorked; //input hours worked
    		double weeklyPay; // weekly pay amount
    		boolean end = false; // is the input name stop?
    		
    		while( true ) // as long as end is false, continue
    		
    		{
    		
    			System.out.print( "Enter Employee's Name:" ); // prompt to enter employee name
    			empName = input.nextLine(); // input employee name
    		
    			if( empName.toLowerCase().equals( "stop" )) // if employee name = stop						
    				return;  // when stop is detected, change the boolean, which ends the while loop
    					
    		while( hourlyRate > 0 ) // while the hourly rate is < 0
    		
    		{	
    			System.out.print( "Enter a positive hourly rate:" ); // print enter a positive hourly rate
    				hourlyRate = input.nextDouble();
    				
    		}
    		
    		while( hoursWorked > 0 ) // while the hours worked are < 0
    		
    		{
    			System.out.print( "Enter a positive number of hours worked:" ); // print enter a positive number of hours worked
    			hoursWorked = input.nextDouble();
    			
    		}
    		
    		weeklyPay = hourlyRate * hoursWorked; // multiply hourly rate by hours worked for weekly pay
    		
    		System.out.printf( "The employee %s was paid $ %.2f this week", empName, weeklyPay ); // print final line
    		System.out.println();
    		
    		cleanInputBuffer = input.nextLine();
    		
    		} // end outer while
    		
    	} // end main method
    	
    } // end class Payroll2

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Communication problems?

    I compiled and ran the program. You still have various problems:
    • You have to initialize the hourlyRate and hoursWorked variables (since you don't know what it is, makes them equal to zero).
    • You have to ask for the employee's hourly rate and the hours worked.. after you get the name of the employee (no need to use a "while" loop to get these values).
    • The program exits perfectly when the employee name is "stop"

    It would look something like:
    • Ask employee name
    • Ask hourly rate
    • Ask hours worked
    • Do calculations

    Make those changes and see what happens.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by CJSLMAN View Post
    Just a clarification for the OP's knowledge...
    • A "break" command will break out of a loop's execution, but will continue with the code execution after the loop.
    • A "return" statement will exit the current method execution.


    In this case, with the OP's code, both have the same effect.

    Luck,
    CJSL
    Thanks for the CJSL. Otherwise our thread starter can confuse on our early explanation.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by jsand2 View Post
    No I dont receive any errors what so ever. I just assumed it was all fine because of that.The only time I receive an error is when I leave a prompt blank and then just press enter. Outside of that it seems to run fine on my end.
    Read the 12th post send by CJSLMAN here in this thread. It's amazing if you don't comes with that error I've explain. The language fundamentals going to mess if so.

Similar Threads

  1. What did i do wrong this time!
    By PureAwesomeness in forum New To Java
    Replies: 28
    Last Post: 01-20-2009, 12:47 AM
  2. Replies: 1
    Last Post: 01-12-2009, 09:59 AM
  3. What's wrong in my program...?
    By Annatar in forum Java Software
    Replies: 3
    Last Post: 10-31-2008, 07:03 AM
  4. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 04:37 AM
  5. what is wrong with this program ?
    By Poor Bee in forum New To Java
    Replies: 1
    Last Post: 05-07-2008, 08:23 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
  •