2 Replies - 523 Views - Last Post: 03 October 2012 - 08:43 AM Rate Topic: -----

#1 d.koen  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-November 10

While loop in Java not displaying properly after input

Posted 14 September 2012 - 03:15 PM

The Java code below is for an assignment that has already been submitted that was supposed to take an employees name, number of hours worked, and the rate of pay and calculate the total pay for the week. Problem is when executed it shows enter employee name: enter number of hours: at the end everytime no matter what is entered and normally above it shows employee name as blank as if it skipped several parts of the loop even though they were entered. I've attached a screenshot of the code at runtime with the errors. I'm at wits end and I know I'm going to lose points but now I just want to know what I've done wrong. Sorry for how sloppy the code is by the way, the indent for the main while loop is off.

import java.util.Scanner;

public class PayRollLoop
{
   public static void main( String[] args )
   {
      Scanner input = new Scanner( System.in);
      
      String name = "";
      float numberOfHours = 0;
      float payRate = 0;
      float totalPay = 0;

      System.out.println("Welcome to Payroll Calculator v2");
      while(!"stop".equals(name))
     {
      name = "";
      numberOfHours = 0;
      payRate = 0;
      totalPay = 0;
      System.out.print("Enter employee name: ");
      name = input.nextLine();
      //loop until number of hours is positive
      while(numberOfHours < 1)
      {
      System.out.print("Enter hours worked: ");
      numberOfHours = input.nextFloat();
      if(numberOfHours < 1)
	System.out.println("please enter a positive number");
      }
      //loop until pay rate is positive
      while(payRate < 1)
      {
      System.out.print("Enter pay rate: ");
      payRate = input.nextFloat();
      if(payRate < 1)
	System.out.println("please enter a positive number");
      }

      totalPay = numberOfHours * payRate;

      System.out.println("------------------------------------------------");
      System.out.printf("Employee name: %s \n", name);
      System.out.printf("Total pay: $%.2f\n", totalPay);
      }
   } //end main()
} //end class PayRollLoop         


Is This A Good Question/Topic? 0
  • +

Replies To: While loop in Java not displaying properly after input

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: While loop in Java not displaying properly after input

Posted 14 September 2012 - 03:27 PM

The Scanner methods that get the next number (int, float, double, etc.) don't read the end of line character. So after you read the float at line 35 (and other places), a linefeed remains in the input buffer. If your next Scanner read looks for the linefeed to signal the end of input, like nextLine(), then it consumes the linefeed in the buffer as the input and moves on. The way to fix this is to add an input.nextLine() after the input.nextFloat() to "flush" the buffer.

After you get that fixed and your program provides the correct prompts, you may see ways to move the statements around so that the prompts appear when they should. Let us know if you need more help.
Was This Post Helpful? 4
  • +
  • -

#3 d.koen  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-November 10

Re: While loop in Java not displaying properly after input

Posted 03 October 2012 - 08:43 AM

Thank you so much, obviously that solved the problem although I added a second getLine() statement to the very end of the loop so upon restarting the loop the buffer is clear.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1