| | |
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:
Solved Threads: 0
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?
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?
Java Syntax (Toggle Plain Text)
import java.util.ArrayList; import java.util.Scanner; // program uses class Scanner class EmployeeData { // start of EmployeeData class EmployeeData(String newName, float newHourly, float newHours) { name = newName; hourly = newHourly; hours = newHours; } public String getName() { return name; } public float getProduct() { return hourly * hours; } private String name; private float hourly, hours; } // end of EmployeeData class public class Payroll4 { // start of public class private String EmployeeData; // employee information for this Payroll private static int ArrayList; private static int i; // main method begins execution of java application public static void main( String args[] ) { // Start main method ArrayList employees = new ArrayList (); // create scanner to obtain input from command window Scanner input = new Scanner ( System.in ); System.out.println(); System.out.print( "Enter employee name or stop to quit: " ); String empName = input.nextLine(); // read employee name while (empName.equalsIgnoreCase("STOP") != true) { EmployeeData employee; float hourly; // Employee Hourly Rate float hours; // Employee Hours Worked float product; // Employee Salary for Week { System.out.print( "Enter hourly rate: " ); // prompt hourly = input.nextFloat(); // read hourly rate from user while (hourly <= 0) { System.out.print( "Please Enter postive hourly rate: "); // prompt for postive hourly rate hourly = input.nextFloat(); // read hourly rate from user } System.out.print( "Enter hours worked for week: "); // prompt for hours hours = input.nextFloat(); //read number of hours worked while (hours <= 0) { System.out.print( "Please enter postive hours worked: "); // prompt for postive hours worked hours = input.nextFloat(); // read number of hours worked } employee = new EmployeeData(empName, hourly, hours) { }; System.out.println(); System.out.print( employee.getName( ) ); // display employee name System.out.printf( "'s Weekly pay: $%,.2f\n", employee.getProduct() ); // Display weekly pay System.out.println(); System.out.println(); System.out.print( "Enter employee name or stop to quit: " ); // prompt for employee name String empName = input.nextLine(); // read employee name } System.out.println(); System.out.println( "Thank you, Goodbye" ); } // end main method }} // end class Payroll4
Last edited by jcato77; Mar 10th, 2008 at 12:14 am.
•
•
Join Date: Mar 2008
Posts: 2
Reputation:
Solved Threads: 1
Java Syntax (Toggle Plain Text)
(empName.equalsIgnoreCase("STOP") != true)
should read:
Java Syntax (Toggle Plain Text)
(empName.equalsIgnoreCase != stop)
Last edited by jamesbien; Mar 10th, 2008 at 12:39 am. Reason: typo
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"?
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. Ahhhh
Ok, your line at the bottom of your code that reads
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
you are defining the variable but also assigning an initial value to it. After that to reassign a new value, you just write
Then when the loop comes back to the top it will check the same variable with the new value.
Ok, your line at the bottom of your code that reads
java Syntax (Toggle Plain Text)
String empName = input.nextLine(); // read employee name
The problem here is that you have already defined empName but you want to assign a new value to it. When you say
java Syntax (Toggle Plain Text)
String empName = input.nextLine();
java Syntax (Toggle Plain Text)
empName = input.nextLine(); // get another line of input
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
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.
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.
Java Syntax (Toggle Plain Text)
package pay_1; import java.util.Scanner; public class Calcu { // Program execution starts here public static void main ( String [] args ) { // Start Scanner Scanner input = new Scanner (System.in); String name; String name2; double payRate; double hoursWorked; // Begin User Input System.out.println("Welcome:-)\nEnter employee's first name\nor type stop to quit"); name = input.next(); // loop until stop begins while ( !name.equals("stop") ) { System.out.println("Enter employee's last name"); name2 = input.next();// read last name from user System.out.println("Enter Pay Rate\n$"); payRate = input.nextFloat(); // read hourly rate from user // start pay rate while loop while (payRate <= 0) { System.out.print( "Please use positive numbers: "); // prompt for input of positive number payRate = input.nextFloat(); // read hourly rate from user } System.out.println("Enter number of hours worked"); hoursWorked = input.nextFloat(); // read hourly rate from user // start hours worked loop for positive number while (hoursWorked <= 0) { System.out.print("please use positive numbers"); // prompt for positive hourly rate hoursWorked = input.nextFloat(); // user input } // Calculations Begin Object product = payRate * hoursWorked; //Display Results System.out.println(name);//print first name System.out.println(name2);//print last name System.out.printf("total income this week is $%.2f\n", product); //1st prompt for user input in loop System.out.println("Enter employee's first name\nor type stop to quit");// name = input.next(); }//end while // goodbye message System.out.println("Thank you for using "your name here"'s payroll program."); }// End main method }// 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
![]() |
Similar Threads
- nested for loop problem (Java)
- My loop won't work, but I'm positive it should. (Python)
- Can't get loop to work properly (C++)
- Random Different Number in each loop ?? HLP Me PLZ (c or c++) (C++)
- Need help with string loop (C)
- Help with gui loop. (C)
Other Threads in the Java Forum
- Previous Thread: Help with Netbeans (n00b inside)
- Next Thread: How To Call Shell Script In Java
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui health html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor





