Why does my program skip a hint in a loop?

My code should cycle continuously until "stop" is entered as the employee name. The problem that I encountered is that after she calculates the first hours of operation and speed, she will again miss the invitation for the employee name. What for? (Code in two separate classes, by the way) 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 Name: "); eName = input.nextLine(); theEmployee.setName(eName); if (eName.equalsIgnoreCase("stop")) { return; } System.out.print("\nEnter Employee 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 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 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); } } 
+4
source share
3 answers

The problem is this:

 eName = input.nextLine(); 

Change this to:

 eName = input.next(); 

He will work.

By default, the java.util.Scanner class starts parsing text as soon as you press the enter key. Therefore, inside the loop you must request input.next(); because the string is already being processed.

See javadoc for both methods.

+6
source

I assume it expects input in input.nextLine() , but the prompt does not appear.

I assume that there is a problem with buffering somewhere - that the invitation prints, but the output does not get on the screen. Try calling System.out.flush() before calling input.nextLine() .

+2
source

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

Rate = input.nextDouble();

In response to this line, you can enter β€œ15.00” and then press {enter}, which will place the β€œnew line” in the input stream, creating the input stream 15.00\n . When retrieving Double, it will leave a β€œnew line” in the input stream.

When trying to request a user for a different employee name, he reads that there is still a β€œnew line” in the input stream, and he immediately returns what was before, this is an empty line.

To clear the input stream, run the dummy .nextLine.

0
source

Source: https://habr.com/ru/post/1340732/


All Articles