program loops never ending
My code compiles correctly. After entering the employee name, it repeats Enter hourly rate without letting me input data. Where have I gone wrong?
Code java:
// Payroll Program Part 2
import java.util.Scanner; //uses class scanner
import java.io.PrintStream;
public class PayrollProgramPart2
{
// begin execution
public static void main( String args[] )
{
// create scanner
Scanner input = new Scanner(System.in );
double number1; //input
double number2; //input
double sum; //sum of 1 and 2
boolean end = false; //is input stop
while (end == false) // end is false proceed
{
number1 = -1; both are -1
number2 = -1; both are -1
String name;
System.out.print( "Enter Employee name:" ); //prompt
name = input.nextLine();
if(name.toLowerCase() == "stop")
end = true; // stop is detected
while loop while(number1<0) // since I set it at -1 ahould loop until a postive number is put in
System.out.print( "Enter hourly rate:" ); //prompt
number1 = input.nextDouble(); // read first number
while(number2 < 0) // same as number1
{
System.out.print( "Enter number of hours:" ); // prompt
number2 = input.nextDouble(); // read second number
}
sum = number1 * number2; //multiply numbers
System.out.printf (" Weekly Pay for%S is $ %.2f\n", name, sum); // Wage
} // end
} // end
} //end
} // end
Re: program loops never ending
Hello Ccogh05.
Welcome to the forums. :)
Whats with all the while loops? Take a look at this example:
Code Java:
import java.util.Scanner;
public class PayrollProgramPart2_jpf {
/**
* JavaProgrammingForums.com
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Employee name:");
while(input.hasNext()){
String strInput = input.next();
if(strInput.equals("stop")){
System.exit(0);
}
String name = strInput;
System.out.print("Enter hourly rate:");
double number1 = input.nextDouble();
System.out.print("Enter number of hours:");
double number2 = input.nextDouble();
double sum = number1 * number2;
System.out.printf("Weekly Pay for%S is $ %.2f\n", name, sum);
}
}
}
Re: program loops never ending
Quote:
Originally Posted by
Ccogh05
My code compiles correctly. After entering the employee name, it repeats Enter hourly rate without letting me input data. Where have I gone wrong?
Code java:
// Payroll Program Part 2
import java.util.Scanner; //uses class scanner
import java.io.PrintStream;
public class PayrollProgramPart2
{
// begin execution
public static void main( String args[] )
{
// create scanner
Scanner input = new Scanner(System.in );
double number1; //input
double number2; //input
double sum; //sum of 1 and 2
boolean end = false; //is input stop
while (end == false) // end is false proceed
{
number1 = -1; both are -1
number2 = -1; both are -1
String name;
System.out.print( "Enter Employee name:" ); //prompt
name = input.nextLine();
if(name.toLowerCase() == "stop")
end = true; // stop is detected
while loop while(number1<0) // since I set it at -1 ahould loop until a postive number is put in
System.out.print( "Enter hourly rate:" ); //prompt
number1 = input.nextDouble(); // read first number
while(number2 < 0) // same as number1
{
System.out.print( "Enter number of hours:" ); // prompt
number2 = input.nextDouble(); // read second number
}
sum = number1 * number2; //multiply numbers
System.out.printf (" Weekly Pay for%S is $ %.2f\n", name, sum); // Wage
} // end
} // end
} //end
} // end
Just a word of advice, you should probably reduce your WHILEs to a minimum... since the higher the number of loops is the more complex your algorithm is. As for the ending problem, whenever you want to check if two strings you need to use the equals methods, == will only check if the objects' reference is the same. So in your case you'd have name.toLowerCase().equals("stop") or name.equalsIgnoreCase("stop'')
Re: program loops never ending
Another way might be to change the while loop to
while(!strInput.equals("stop"))
and get rid of if block.
Re: program loops never ending
ok i agree with the previous posts with getting rid of the while statements.
but as is, i found your error...
Code java:
while loop while(number1<0)
// there needs to be a { here, otherwise only the next line is associated with the while loop
System.out.print( "Enter hourly rate:" );
number1 = input.nextDouble();
// and here needs to be the } so the program knows that this is the intended stop point
so basically when the program reads that number1<0 it then goes on to read the System.out.print line, and since there is no brackets the program assumes that is the only line you want to associate with the while loop therefore the next line is never reached and you have created a forever loop
hope that helps
Re: program loops never ending
I agree with previous posts. Too many while loops complicate your algorithm.