My code is supposed to continuously loop until "stop" is entered as employee name. A problem I am having is that once it does the calculation for the first employee's hours and rate, it will skip over the prompt for employee name again. Why? (The code is in 2 separate classes, btw) Please help. Here is my code:

package payroll_program_3;
import java.util.Scanner;

public class payroll_program_3
{
    public static void main(String[] args)
    {

        Scanner input = new Scanner( System.in );

        employee_info theEmployee = new employee_info();

        String eName = "";
        double Hours = 0.0;
        double Rate = 0.0;
        while(true)
        {
            System.out.print("\nEnter Employee's Name: ");
            eName = input.nextLine();
            theEmployee.setName(eName);
            if (eName.equalsIgnoreCase("stop"))
            {
                return;
            }

            System.out.print("\nEnter Employee's Hours Worked: ");
            Hours = input.nextDouble();
            theEmployee.setHours(Hours);
            while (Hours <0)    //By using this statement, the program will not
            {                   //allow negative numbers.
                System.out.printf("Hours cannot be negative\n");
                System.out.printf("Please enter hours worked\n");
                Hours = input.nextDouble();
                theEmployee.setHours(Hours);
            }

            System.out.print("\nEnter Employee's Rate of Pay: ");
            Rate = input.nextDouble();
            theEmployee.setRate(Rate);
            while (Rate <0)    //By using this statement, the program will not
            {                  //allow negative numbers.
                System.out.printf("Pay rate cannot be negative\n");
                System.out.printf("Please enter hourly rate\n");
                Rate = input.nextDouble();
                theEmployee.setRate(Rate);
            }

            System.out.print("\n Employee Name:     " + theEmployee.getName());
            System.out.print("\n Employee Hours Worked:     " + theEmployee.getHours());
            System.out.print("\n Employee Rate of Pay:     " + theEmployee.getRate() + "\n\n");
            System.out.printf("\n %s's Gross Pay: $%.2f\n\n\n", theEmployee.getName(),
                theEmployee.calculatePay());
        }
    }
}

AND

package payroll_program_3;

        public class employee_info
{
            String employeeName;
            double employeeRate;
            double employeeHours;

public employee_info()
    {
    employeeName = "";
    employeeRate = 0;
    employeeHours = 0;
    }

public void setName(String name)
    {
    employeeName = name;
    }

public void setRate(double rate)
    {
    employeeRate = rate;
    }

public void setHours(double hours)
    {
    employeeHours = hours;
    }

public String getName()
    {
    return employeeName;
    }

public double getRate()
    {
    return employeeRate;
    }

public double getHours()
    {
    return employeeHours;
    }

public double calculatePay()
    {
    return (employeeRate * employeeHours);
    }
}
有帮助吗?

解决方案

The problem is at:

eName = input.nextLine();

Change this to:

eName = input.next();

It will work.

By default java.util.Scanner class starts parsing the text as soon as you hit the enter key. So inside the loop you should be asking for input.next(); as a line is already being processed.

See javadoc for both the methods.

其他提示

I assume that it is waiting for input at input.nextLine() but the prompt does not appear.

My guess is that there is a buffering issue somewhere — that the prompt is being printed but the output is not getting to the screen. Try calling System.out.flush() before calling input.nextLine().

Never used Java in my life, but here it goes...

Rate = input.nextDouble();

In response to this line, you might type "15.00" and then hit {enter}, which will put a "new line" in the input stream, making the input stream 15.00\n. When extracting the Double, it will leave the "new line" still in the input stream.

When you attempt to prompt the user for another employee's name, it reads that there is still a "new line" in the input stream, and it will immediately return what was prior to that, which is an empty string.

In order to clear out the input stream, run a dummy .nextLine.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top