/* Check Point: Payroll Program Part 2
Coded by Curtis Allyn Green
Friday, May 28th 2010 */
//import Scanner object from java.util package
import java.util.Scanner;
//declare the class
public class Payroll2
{
//declare the main method
public static void main(String args[])
{
//declare the method's variables
String empName;
double rate;
double hours;
int more = 1;
//Create an instance of a Scanner object to gather input from the user
Scanner input = new Scanner(System.in);
//Create execution loop that terminates with an empName == "stop"
while(more != 0)
{
//Prompt user to input values for each variable and retrieve them from input
System.out.print("Enter Employee's Name or Enter stop to exit: ");
empName = input.next();
if(empName == "stop")
more = 0;
//Check for continue
if(more == 1)
{
System.out.print("Enter Employee's Hourly Wage: ");
rate = input.nextDouble();
//Ensure that rate is a positive value
if(rate <= 0)
{
while(rate <= 0)
{
System.out.print("Error non-positive input. Please enter a value greater than 0 for Employee's Wage: ");
rate = input.nextDouble();
}
}
System.out.print("Enter The Hours Worked By Employee: ");
hours = input.nextDouble();
//Ensure that hours is a positive value
if(hours <= 0)
{
while(hours <= 0)
{
System.out.print("Error non-positive input. Please enter a value greater than 0 for Employee's Hours: ");
hours = input.nextDouble();
}
}
//Display the results
System.out.printf("Employee: %s Net Pay: $%.2f\n", empName, (rate*hours));
}//End continue check
}//End execution while loop
}//End main method
}//End class
Infinite loop.... Why?!
Started by SolidState, May 28 2010 11:37 AM
3 replies to this topic
#1
Posted 28 May 2010 - 11:37 AM
So I am working on this program for my Java Course, and am very perplexed as to why I have an infinite loop... Can someone please help me figure this out? Code is posted below:
|
|
|
#2
Posted 28 May 2010 - 01:30 PM
Hey, SolidState.
I think that the problem is with line 32:
As for fixing line 32, you should use the method equals or equalsIgnoreCase to check for the same String. Using == is best for char's, ints, etc.
Here's the corrected line:
equalsIgnoreCase is a better method too, because it can check for STOP, SToP, etc.
I think that the problem is with line 32:
if(empName == "stop"), but I haven't tested to see if it would fix the issue. I'm guessing that the infinite loop is coming from an incorrect condition, which is why I recommend using booleans instead of 0 and 1, but I guess it's the same exact idea and a matter of taste.
As for fixing line 32, you should use the method equals or equalsIgnoreCase to check for the same String. Using == is best for char's, ints, etc.
Here's the corrected line:
if(empName.equalsIgnoreCase("stop"));
equalsIgnoreCase is a better method too, because it can check for STOP, SToP, etc.
#3
Posted 28 May 2010 - 02:33 PM
Thank you OHHHHH SOOO MUCH, xWalnut! This was killing my brain since I was at a total loss for why this was happening. Now I can submit my program without using the instructor's example, which makes me so happy, and I also learned about a series of interesting methods for manipulating string within the java.lang package. Thanks for the help!
#4
Posted 29 May 2010 - 07:36 AM
== compares references i think correct me if i am wrong , you can use the equals function :) , if ( name.equals("stop") ) //do something


Sign In
Create Account


Back to top









