| Author |
Message |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 10/19/2008 11:54:45
|
Majinterry
Newbie
Joined: 10/19/2008 11:20:54
Messages: 5
Offline
|
Hi, I am in a introduction to java class and I am having a bit of trouble figuring out how to write this while loop. The program needs to repeat itself over and over again until the user enters done at the prompt to enter the employee's name. Could anyone give me some suggestions about where I am going wrong here? Thanks in advance for any help you guys could give me.
//Payroll1 Java Program
//Program to calculate and employess pay based on hourly rate and hours worked.
//Terry Wise 10-17-08
import java.util.Scanner;//Program uses utility scanner
public class Payroll1
{
// Main method begins execution of Java application.
public static void main( String args[] )
{
// Create scanner to recieve input
Scanner input = new Scanner( System.in );
double rate; //employees rate of pay
double hours; //numbers of hours worked
double pay; //equals the employees rate of pay
String newname = "stop";
while ( !newname.equals( "stop" ) );
{
System.out.print( "Enter the employee's name or stop to quit: " ); //prompt for employees name
newname = input.nextLine();
System.out.println(); // Makes space between inputs
System.out.print( "Enter the employee's rate of pay: " ); //prompt for rate of pay
rate = input.nextDouble(); //Get rate of pay from user
System.out.println(); // Makes space between inputs
System.out.print( "Enter the numbers of hours worked: " ); //promt for hours
hours = input.nextDouble(); // Get hours worked from user
System.out.println(); // Makes space between inputs
pay = rate * hours; //find employees pay
System.out.printf( newname ); //Prints the employee's name
System.out.printf( "'s pay is $%.2f", pay ); // Displays the employee's rate of pay
System.out.println();
}
System.out.println( "Goodbye" );
} // end method main
} // end class Payroll1
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 10/19/2008 12:08:53
|
Majinterry
Newbie
Joined: 10/19/2008 11:20:54
Messages: 5
Offline
|
My apologies the While statement in my code reads
While ( newname != "stop" )
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 10/19/2008 14:36:41
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
I would try this just a bit differently. Something like:
And to compare Strings the code above is what you want. Using the "==" or "!=" does not work as that only compares the addresses of the Strings, not the contents.
This message was edited 3 times. Last update was at 10/19/2008 14:39:40
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 10/19/2008 21:16:20
|
Majinterry
Newbie
Joined: 10/19/2008 11:20:54
Messages: 5
Offline
|
Okay here is what I have now, but when I try to compile it I get this message:
Payroll1.java:26: break outside switch or loop
break
^
1 error
//Payroll1 Java Program
//Program to calculate and employess pay based on hourly rate and hours worked.
//Terry Wise 10-17-08
import java.util.Scanner;//Program uses utility scanner
public class Payroll1
{
// Main method begins execution of Java application.
public static void main( String args[] )
{
// Create scanner to recieve input
Scanner input = new Scanner( System.in );
double rate; //employees rate of pay
double hours; //numbers of hours worked
double pay; //equals the employees rate of pay
String newname = null;
while ( true )
{
System.out.print( "Enter the employee's name or stop to quit: " ); //prompt for employees name
newname = input.nextLine();
System.out.println(); // Makes space between inputs
if ( newname.equals( "stop" ) )
break;
}
System.out.print( "Enter the employee's rate of pay: " ); //prompt for rate of pay
rate = input.nextDouble(); //Get rate of pay from user
System.out.println(); // Makes space between inputs
System.out.print( "Please, enter a positive number: " ); //prompt for rate of pay
rate = input.nextDouble(); //Get rate of pay from user
System.out.println(); // Makes space between inputs
System.out.print( "Enter the numbers of hours worked: " ); //promt for hours
hours = input.nextDouble(); // Get hours worked from user
System.out.println(); // Makes space between inputs
pay = rate * hours; //find employees pay
System.out.printf( newname ); //Prints the employee's name
System.out.printf( "'s pay is $%.2f", pay ); // Displays the employee's rate of pay
System.out.println();
} // end method main
} // end class Payroll1
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 10/20/2008 08:01:13
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
I copied the code you have exactly and did not get a syntax error. But it won't do what you want. You'll want to wrap the other areas where you read the rest of the variables in the same while loop.
Like this:
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 10/22/2008 18:20:44
|
Majinterry
Newbie
Joined: 10/19/2008 11:20:54
Messages: 5
Offline
|
I keep getting the statement that I am am using break outside of a switch or loop? Do you know how to fix this?
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 10/22/2008 19:57:11
|
Majinterry
Newbie
Joined: 10/19/2008 11:20:54
Messages: 5
Offline
|
Okay I have the program working the way I want except for one thing. It works fine the first time through the loop, but the second time it skips the step where I input the name, can you see what I've done wrong that would cause this?
//Payroll1 Java Program
//Program to calculate and employess pay based on hourly rate and hours worked.
//Terry Wise 10-21-08
import java.util.Scanner;//Program uses utility scanner
public class Payroll4
{
// Main method begins execution of Java application.
public static void main( String args[] )
{
// Create scanner to recieve input
Scanner input = new Scanner( System.in );
double rate; //employees rate of pay
double hours; //numbers of hours worked
double pay; //equals the employees rate of pay
String newname = null;
while (true) { // creates loop that runs until stop is entered.
// get Name
System.out.println("Enter the name of the employee or stop to exit:");
newname = input.nextLine();
if (newname.equals("stop")) break; // exits the program if stop is entered.
// get Rate
System.out.println("Enter the hourly rate:");// Gets employees rate of pay
rate = input.nextDouble();
while (rate <= 0) { // Until we gate a valid rate
System.out.println("Invalid rate. Please reenter the rate:");
rate = input.nextDouble();
}
// get Hours
System.out.println("Enter the hours:");
hours = input.nextDouble();
while (hours <= 0) { // Until we get valid hours
System.out.println("Invalid hours. Please reenter the hours:");
hours = input.nextDouble();
}
pay = rate * hours; //find employees pay
System.out.printf( newname ); //Prints the employee's name
System.out.printf( "'s pay is $%.2f", pay ); // Displays the employee's rate of pay
System.out.println();
}
} // end method
}// end class
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 10/23/2008 13:22:21
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
Change the code:
to
the nextLine() call seems to not do what you want.
|
Thanks for using the forums at hotjoe.com |
|
|
 |
|
|