Pregunta

I'm writing a small payroll program with 2 classes (Payroll/contains main, and Employee). The program should continue to ask for user input until the user enters stop as an employee's name. I was able to get the program to terminate after entering stop as the first employee's name; however, when I input an actual name (for example Jim), the program runs through the while loop once fine. The problem is that when the while loop runs again it does not allow any user input for the employee name. It jumps directly to the next line, which is asking for hours worked. Here is my code from the Employee class.

package payroll;

import java.util.Scanner;

public class Employee {

    // class variables
    private String employeeName;
    private double hoursWorked;
    private double payRate;
    private double weeklyPay;

    // Employee class constructor
    public Employee() {

    }

    // get value of class variable employeeName from user
    public void getInfo() {

        String name = ""; // initialized as empty string for use in while loop
        String term = "stop"; // terminating condition for while loop
        double hours; // employee hours worked
        double rate; // employee pay rate

        Scanner input = new Scanner(System.in);

        while (!name.equals(term)) { // will continue as long as name does not equal "stop"

            System.out.print("Enter the employee's name or stop if finished: ");
            name = input.nextLine(); // assigns the value input for name
            employeeName = name;

            if (employeeName.equals(term))
                break; // terminate while loop if user enters "stop"

            System.out.print("How many hours did " + employeeName + " work? ");
            hours = input.nextDouble(); // assigns the value input for hours
            hoursWorked = hours; // assigns hours to Employee class variable hoursWorked

            if (hours < 0) { // control statement to make sure hours is not a negative number
                System.out.print("Hours cannot be a negative number.\n");
                System.out.print("How many hours did " + employeeName + " work? ");
                hours = input.nextDouble(); // assigns the value input for hours
                hoursWorked = hours; // assigns hours to Employee class variable hoursWorked
            }

            System.out.print("Enter " + employeeName + "'s pay rate: ");
            rate = input.nextDouble(); // assigns the value input for rate
            payRate = rate; // assigns rate to Employee class variable payRate

            if (rate < 0) {
                System.out.print("Rate cannot be a negative number.\n");
                System.out.print("Enter " + employeeName + "'s pay rate: ");
                rate = input.nextDouble(); // assigns the value input for rate
                payRate = rate; // assigns rate to Employee class variable payRate
            }

            // calculates employee pay form calcPay method
            calcPay();

            // outputs message to console
            displayMessage();
        }
    }

public void calcPay() {

        weeklyPay = setHours() * setRate();
}

public void displayMessage() {

        System.out.printf("%s%s%.2f \n", employeeName, "'s weekly pay is $", weeklyPay);
}

This is what my output looks like:

Enter the employee's name or stop if finished: Tim

How many hours did Tim work? 40

Enter Tim's pay rate: 20.50

Tim's weekly pay is $820.00

Enter the employee's name or stop if finished: How many hours did work?

I can't understand why I am not enter a new employee's name.

¿Fue útil?

Solución

After consuming your Double for the pay rate, you need to consume the line feed before reading the next Employee name.

Thus, simply adding input.nextLine() just before the end of your while loop should do the trick.


Sample piece of code:

while (!name.equals(term)) { // will continue as long as name does not equal "stop"
    System.out.print("Enter the employee's name or stop if finished: ");
    name = input.nextLine(); // assigns the value input for name

    // all your stuff... 

    System.out.print("Enter " + employeeName + "'s pay rate: ");
    rate = input.nextDouble(); // assigns the value input for rate

    // some more stuff... 

    if(input.hasNextLine()) {
        input.nextLine();
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top