PDA

View Full Version : why doesn't this work (java)



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.

tesco
11-03-2008, 12:43 AM
The problem is you're asking for the name after the loop starts.
So if you enter 'stop' as the name it won't take effect until next time through the loop.
Read: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html


I've never done java before but I think you can use something like this:



// Week two check point
// Thomas Gamache IT-215

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 ("");

do
{ // open loop

System.out.println( "Please enter employee name or stop to quit:" ); // input employee name
EmployeeName = input.nextLine();

if (!EmployeeName.equalsIgnoreCase ("stop"))
{
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
}
} // end of loop
while (!EmployeeName.equalsIgnoreCase ("stop"));
} // end of method
} //end of class
Even if it doesn't work at least it gets you in the right direction...

I've also properly tabbed everything for you to make it easier to read.
You then don't need the "// open loop" and "// end loop", etc. because you just follow the lines vertically.

s_player
11-09-2008, 03:20 PM
Also you can do this:

while(true)
{
System.out.println("Please enter employee name or stop to quit:");
EmployeeName = input.nextLine();
if(EmployeeName.equalsIgnoreCase("stop"))
break;

System.out.println("Please enter employee pay rate:");
// ...
}