Java Programming Loop Execution Problem [Solved/Resolved]
Hey guys,
I am most definitely new here and new to Java. I have been trying to get help with my code skipping executing a specific function when I run the program. The first time through, everything executes perfectly, that is until the loop goes into effect. It skips over allowing the user to input "Employee Name" which is extremely important, because this is the are where the user can input "stop" to exit. Anyone know a different method to use or where a conflict could be occuring to cause this problem?
Here's the code:
Code Java:
package payrollprogram2;
import java.util.Scanner;
public class PayrollProgram2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.printf("Welcome to the Employee Payroll System!\n");
String name;
double rate;
int hours;
boolean g = true;
while (g == true)
{
System.out.printf("Enter employee's name. Type 'stop' to exit.\n");
name = input.nextLine();
if(name.equals("stop"))
{System.out.printf("Exiting. Good-bye!");
g = false;
break;}
else
{
do
{
System.out.printf("Enter the hourly rate.\n");
rate = input.nextDouble();
if(rate <= 0)
System.out.printf("Please enter a positive number (above zero).\n");
}while(rate <= 0);
do
{
System.out.printf("Enter the amount of hours worked (Whole, positive numbers only).\n");
hours = input.nextInt();
if(hours <= 0)
System.out.printf("Please enter a positive number (above zero).\n");
}while(hours <= 0);
int overtime;
if (hours <= 40)
{
overtime = 0;
}
else
{
overtime = hours - 40;
}
double payroll = hours * rate + overtime * rate/2;
System.out.printf("\nEmployee Name: %s\n", name);
System.out.printf("Hours Worked: %d\n", hours);
System.out.printf("Rate per Hour: $%.2f\n", rate);
System.out.printf("Total Income: $%.2f\n", payroll);
}
}
}
}
Re: Java Programming Loop Execution Problem
This is actually a pretty quick fix, but the reason the problem is occurring can be illusive. The methods nextInt(), nextDouble(), nextBoolean()...etc. don't read anything more than what they need to. For instance, if a double is to be read and you type 5.56 and hit enter then the double 5.56 is read correctly but not the newline character (caused by enter being pressed) which is left in the input buffer. Thus, if you try to read the line afterwards it will see the new line character left over in the buffer and return a empty string. To rectify this you need to call nextLine() which will read the unwanted newline character and set the position at the beginning of the next line.
Re: Java Programming Loop Execution Problem
Quote:
Originally Posted by
KucerakJM
This is actually a pretty quick fix, but the reason the problem is occurring can be illusive. The methods nextInt(), nextDouble(), nextBoolean()...etc. don't read anything more than what they need to. For instance, if a double is to be read and you type 5.56 and hit enter then the double 5.56 is read correctly but not the newline character (caused by enter being pressed) which is left in the input buffer. Thus, if you try to read the line afterwards it will see the new line character left over in the buffer and return a empty string. To rectify this you need to call nextLine() which will read the unwanted newline character and set the position at the beginning of the next line.
I got it :D from what you said, I was able to research more thoroughly and found others instances that said to call input.nextLine(); directly after my last input.nextInt();
Thank you so very much!!!!