Help - While loop won't work

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 21
Reputation: jcato77 is an unknown quantity at this point 
Solved Threads: 0
jcato77 jcato77 is offline Offline
Newbie Poster

Help - While loop won't work

 
0
  #1
Mar 10th, 2008
Hello,

The following is background on my assignment "Modify payroll program so that it uses a class to store and retrive the employee's name, hourly rate and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Make sure the code is readable and well documented."

I have everything coded but my while loop for my program exit value is not working. Can someone let me know what I'm missing?

  1. import java.util.ArrayList;
  2. import java.util.Scanner; // program uses class Scanner
  3.  
  4. class EmployeeData { // start of EmployeeData class
  5.  
  6. EmployeeData(String newName, float newHourly, float newHours) {
  7. name = newName; hourly = newHourly; hours = newHours;
  8. }
  9.  
  10. public String getName() { return name; }
  11.  
  12. public float getProduct() { return hourly * hours; }
  13.  
  14. private String name;
  15.  
  16. private float hourly, hours;
  17.  
  18. } // end of EmployeeData class
  19.  
  20. public class Payroll4
  21.  
  22. { // start of public class
  23.  
  24. private String EmployeeData; // employee information for this Payroll
  25.  
  26. private static int ArrayList;
  27.  
  28. private static int i;
  29.  
  30. // main method begins execution of java application
  31. public static void main( String args[] )
  32. { // Start main method
  33.  
  34. ArrayList employees = new ArrayList ();
  35.  
  36. // create scanner to obtain input from command window
  37. Scanner input = new Scanner ( System.in );
  38.  
  39. System.out.println();
  40. System.out.print( "Enter employee name or stop to quit: " ); String empName = input.nextLine(); // read employee name
  41.  
  42. while (empName.equalsIgnoreCase("STOP") != true)
  43.  
  44. {
  45.  
  46. EmployeeData employee;
  47. float hourly; // Employee Hourly Rate
  48. float hours; // Employee Hours Worked
  49. float product; // Employee Salary for Week
  50.  
  51. {
  52. System.out.print( "Enter hourly rate: " ); // prompt
  53. hourly = input.nextFloat(); // read hourly rate from user
  54.  
  55. while (hourly <= 0)
  56. {
  57. System.out.print( "Please Enter postive hourly rate: "); // prompt for postive hourly rate
  58. hourly = input.nextFloat(); // read hourly rate from user
  59. }
  60.  
  61. System.out.print( "Enter hours worked for week: "); // prompt for hours
  62. hours = input.nextFloat(); //read number of hours worked
  63.  
  64. while (hours <= 0)
  65. {
  66. System.out.print( "Please enter postive hours worked: "); // prompt for postive hours worked
  67. hours = input.nextFloat(); // read number of hours worked
  68. }
  69.  
  70. employee = new EmployeeData(empName, hourly, hours) {
  71.  
  72. };
  73.  
  74. System.out.println();
  75. System.out.print( employee.getName( ) ); // display employee name
  76. System.out.printf( "'s Weekly pay: $%,.2f\n", employee.getProduct() ); // Display weekly pay
  77. System.out.println();
  78.  
  79. System.out.println();
  80. System.out.print( "Enter employee name or stop to quit: " ); // prompt for employee name
  81. String empName = input.nextLine(); // read employee name
  82. }
  83.  
  84. System.out.println();
  85. System.out.println( "Thank you, Goodbye" );
  86.  
  87. } // end main method
  88.  
  89. }} // end class Payroll4
Last edited by jcato77; Mar 10th, 2008 at 12:14 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: jamesbien is an unknown quantity at this point 
Solved Threads: 1
jamesbien jamesbien is offline Offline
Newbie Poster

Re: Help - While loop won't work

 
0
  #2
Mar 10th, 2008
  1. (empName.equalsIgnoreCase("STOP") != true)

should read:
  1. (empName.equalsIgnoreCase != stop)
Last edited by jamesbien; Mar 10th, 2008 at 12:39 am. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 796
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Help - While loop won't work

 
0
  #3
Mar 10th, 2008
Hi jcato77 and welcome to DaniWeb,

jamesbien is completely wrong with his advice. But I am not sure about your question. Do you mean that the code inside the loop that jamesbien referenced never executes, or that it executes even when the user inputs the word "STOP"?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 21
Reputation: jcato77 is an unknown quantity at this point 
Solved Threads: 0
jcato77 jcato77 is offline Offline
Newbie Poster

Re: Help - While loop won't work

 
0
  #4
Mar 10th, 2008
Hello darkagn

I am able to enter the first Name and Hours worked and payrate, when the loop continues it does not ask for empName again. If I place the same empName request at the end of my program it does not complie. I'm told empName has already been defined
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 796
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Help - While loop won't work

 
0
  #5
Mar 10th, 2008
Ahhhh

Ok, your line at the bottom of your code that reads
  1. String empName = input.nextLine(); // read employee name
remove the word "String" from this line.

The problem here is that you have already defined empName but you want to assign a new value to it. When you say
  1. String empName = input.nextLine();
you are defining the variable but also assigning an initial value to it. After that to reassign a new value, you just write
  1. empName = input.nextLine(); // get another line of input
Then when the loop comes back to the top it will check the same variable with the new value.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 21
Reputation: jcato77 is an unknown quantity at this point 
Solved Threads: 0
jcato77 jcato77 is offline Offline
Newbie Poster

Re: Help - While loop won't work

 
0
  #6
Mar 10th, 2008
Thank you for the advice, I was able to fix it off of your comments and a little more research through my books.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: K504K is an unknown quantity at this point 
Solved Threads: 0
K504K K504K is offline Offline
Newbie Poster

Re: Help - While loop won't work

 
0
  #7
Apr 13th, 2008
This is my first post, and I am a java noob. I had a similar assignment to complete this weekend. After reading this post I solved the problem. The while loops condition has to be met, and it has to have an input to tell it not to loop again. I hope that makes sense, but here is what I have for my code.
  1. package pay_1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Calcu {
  6.  
  7.  
  8. // Program execution starts here
  9. public static void main ( String [] args )
  10. {
  11. // Start Scanner
  12. Scanner input = new Scanner (System.in);
  13. String name;
  14. String name2;
  15. double payRate;
  16. double hoursWorked;
  17.  
  18.  
  19.  
  20.  
  21. // Begin User Input
  22. System.out.println("Welcome:-)\nEnter employee's first name\nor type stop to quit");
  23. name = input.next();
  24.  
  25. // loop until stop begins
  26. while ( !name.equals("stop") )
  27. {
  28.  
  29. System.out.println("Enter employee's last name");
  30. name2 = input.next();// read last name from user
  31.  
  32. System.out.println("Enter Pay Rate\n$");
  33. payRate = input.nextFloat(); // read hourly rate from user
  34.  
  35. // start pay rate while loop
  36. while (payRate <= 0)
  37. {
  38. System.out.print( "Please use positive numbers: "); // prompt for input of positive number
  39. payRate = input.nextFloat(); // read hourly rate from user
  40. }
  41.  
  42. System.out.println("Enter number of hours worked");
  43. hoursWorked = input.nextFloat(); // read hourly rate from user
  44.  
  45. // start hours worked loop for positive number
  46. while (hoursWorked <= 0)
  47. {
  48. System.out.print("please use positive numbers"); // prompt for positive hourly rate
  49. hoursWorked = input.nextFloat(); // user input
  50. }
  51.  
  52. // Calculations Begin
  53. Object product = payRate * hoursWorked;
  54.  
  55. //Display Results
  56. System.out.println(name);//print first name
  57. System.out.println(name2);//print last name
  58. System.out.printf("total income this week is $%.2f\n", product);
  59.  
  60. //1st prompt for user input in loop
  61. System.out.println("Enter employee's first name\nor type stop to quit");//
  62. name = input.next();
  63.  
  64.  
  65. }//end while
  66. // goodbye message
  67. System.out.println("Thank you for using "your name here"'s payroll program.");
  68. }// End main method
  69.  
  70.  
  71. }// End pay_1

I noticed that many of the similar assignments on the net are a little more advanced than mine is, so I will probably post solutions that I . This one works its not perfect, but I hope it helps you find the answer to your problem. Finally thank you to the Dani Web community for helping me solve this and many other problems.
Last edited by K504K; Apr 13th, 2008 at 3:47 am. Reason: Code will not display properly
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC