4

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);
    }
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
g3n3rallyl0st
  • 111
  • 3
  • 4
  • 11

3 Answers3

6

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.

Favonius
  • 13,959
  • 3
  • 55
  • 95
2

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().

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

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.

TimFoolery
  • 1,895
  • 1
  • 19
  • 29
  • well this is not how scanner class works. the newline `\n` is the default delimiter and is not counted in for any processing, other than defining `line` limits. – Favonius Feb 22 '11 at 04:55
  • According to dozens of reputable sites, this is exactly how it works. – TimFoolery Feb 22 '11 at 08:53
  • I don't know of any reputable site but please check the javadoc for `java.util.Scanner` where they have given regular expression based grammar to parse the **Number Syntax**. Towards the end it is clearly mentioned that `Whitespace is not significant in the above regular expressions`. Also pls hav a look at the src of `private void readInput()` and `private String getCompleteTokenInBuffer(Pattern pattern)` which `returns a "complete token" that matches the specified pattern A token is complete if surrounded by delims; a partial token is prefixed by delims but not postfixed by them`. – Favonius Feb 22 '11 at 11:40
  • Also please have a look at `private boolean hasTokenInBuffer()` which `Returns true if a complete token or partial token is in the buffer. It is not necessary to find a complete token since a partial token means that there will be another token with or without more input.` Also the delims are skipped here `if (matcher.lookingAt()) position = matcher.end();`. – Favonius Feb 22 '11 at 11:50