Results 1 to 14 of 14
- 01-24-2009, 07:01 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
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
- 01-24-2009, 07:15 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Here in this line,
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.Java Code:end = true;
- 01-24-2009, 07:59 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
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.
- 01-24-2009, 08:36 PM #4
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-25-2009, 03:46 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Or else you can use the return statement like mentioned below.
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?Java Code:if( empName.toLowerCase().equals( "stop" )) return;
- 01-25-2009, 04:39 AM #6
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-25-2009, 04:52 AM #7
- 01-25-2009, 04:47 PM #8
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
- 01-25-2009, 04:56 PM #9
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
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.
- 01-25-2009, 05:39 PM #10
Comments...
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 toJava Code:if( empName.toLowerCase().equals( "stop" )) return;
Now ... about the two errors that Eranga found...Java Code:while(true) {....
What the above means is that the "while" loops will execute while hourlyRate and hoursWorked are less than zero !!!Java Code:while( hourlyRate [B][COLOR="Red"]<[/COLOR][/B] 0 ) ... while( hoursWorked [B][COLOR="red"]<[/COLOR][/B] 0 )
I think that you wanted to code the following:
Did this help any?Java Code:while( hourlyRate [B][COLOR="Blue"]>[/COLOR][/B] 0 ) ... while( hoursWorked [B][COLOR="blue"]>[/COLOR][/B] 0 )
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-25-2009, 06:17 PM #11
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
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
- 01-25-2009, 06:32 PM #12
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-26-2009, 03:06 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-26-2009, 03:10 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
What did i do wrong this time!
By PureAwesomeness in forum New To JavaReplies: 28Last Post: 01-19-2009, 11:47 PM -
Getting FileNotFoundException in my java program after running for some time
By satya_vanimireddy in forum New To JavaReplies: 1Last Post: 01-12-2009, 08:59 AM -
What's wrong in my program...?
By Annatar in forum Java SoftwareReplies: 3Last Post: 10-31-2008, 06:03 AM -
Simple Addition Program Outputting Wrong Value
By carlodelmundo in forum New To JavaReplies: 4Last Post: 08-05-2008, 03:37 AM -
what is wrong with this program ?
By Poor Bee in forum New To JavaReplies: 1Last Post: 05-07-2008, 07:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks