bornwithnoname
11-02-2008, 11:19 PM
I am going to post some code.. If someone could explain why it doesn't work the way I think it should it would be helpful. My comments will be in blue.
import java.util.Scanner; // program uses Scanner
public class WeekTwoCheckPoint {
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
float PayRate = 0;
float HoursWorked = 0;
float GrossPay;
String EmployeeName = new String ("");
while (!EmployeeName.equalsIgnoreCase ("stop")) At this point I have established the program should only loop if EmployeeName does not equal "stop" What happens is if I enter stop the loop runs once then stops. Why?
{ // open loop
System.out.println( "Please enter employee name or stop to quit:" ); // input employee name
EmployeeName = input.nextLine();
System.out.println( "Please enter employee pay rate:" ); //input emplyee rate
PayRate = input.nextFloat();
System.out.println( "Please enter hours worked:" ); //input hours worked
HoursWorked = input.nextFloat();
GrossPay = HoursWorked * PayRate; // calculate gross pay
if(PayRate > 0 && HoursWorked > 0)
{
System.out.printf(EmployeeName + "'s gross pay is $" + "%6.2f",GrossPay);
}
else
{
System.out.println("Really? A negitive number? Let's assume that was an accident and try a positive number");
}
System.out.println(); // insert blank line
System.out.println(); // insert blank line
System.out.println(); // insert blank line
At this point the loop has completed and should start again as long as the loop flag has not been met. What happens is the loop starts again, but I can't enter the employee name data. It prints the line then goes right to enter payrate which does work, but I am stuck in a loop.
It looks like this
Please enter employee name or stop to quit:
tom
Please enter employee pay rate:
5
Please enter hours worked:
5
tom's gross pay is $ 25.00
Please enter employee name or stop to quit:
Please enter employee pay rate:
I can input data here
} // end of loop
} // end of method
} //end of class
Thanks for any insight you can share.
import java.util.Scanner; // program uses Scanner
public class WeekTwoCheckPoint {
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
float PayRate = 0;
float HoursWorked = 0;
float GrossPay;
String EmployeeName = new String ("");
while (!EmployeeName.equalsIgnoreCase ("stop")) At this point I have established the program should only loop if EmployeeName does not equal "stop" What happens is if I enter stop the loop runs once then stops. Why?
{ // open loop
System.out.println( "Please enter employee name or stop to quit:" ); // input employee name
EmployeeName = input.nextLine();
System.out.println( "Please enter employee pay rate:" ); //input emplyee rate
PayRate = input.nextFloat();
System.out.println( "Please enter hours worked:" ); //input hours worked
HoursWorked = input.nextFloat();
GrossPay = HoursWorked * PayRate; // calculate gross pay
if(PayRate > 0 && HoursWorked > 0)
{
System.out.printf(EmployeeName + "'s gross pay is $" + "%6.2f",GrossPay);
}
else
{
System.out.println("Really? A negitive number? Let's assume that was an accident and try a positive number");
}
System.out.println(); // insert blank line
System.out.println(); // insert blank line
System.out.println(); // insert blank line
At this point the loop has completed and should start again as long as the loop flag has not been met. What happens is the loop starts again, but I can't enter the employee name data. It prints the line then goes right to enter payrate which does work, but I am stuck in a loop.
It looks like this
Please enter employee name or stop to quit:
tom
Please enter employee pay rate:
5
Please enter hours worked:
5
tom's gross pay is $ 25.00
Please enter employee name or stop to quit:
Please enter employee pay rate:
I can input data here
} // end of loop
} // end of method
} //end of class
Thanks for any insight you can share.