0

I've been at this for a while now, and cant seem to figure out why it is behaving this way. After the program runs through the first time it doesn't wait for input for the division name prompt. I know its some form of syntactical error on my behalf, I just can't for the life of me figure it out.

//payroll
import java.util.Scanner;

public class Payroll
{
public static void main(String[] args)
{
    //create scanner object for inputs
    Scanner input = new Scanner(System.in);
    String name = "Null";
    //create instance of class Division
    Division newDivision1 = new Division();

    //prompt for new division name.
    do {
        System.out.println( "Please Enter the Division Name. Enter \"stop\" to exit: " );
        name = input.nextLine();
        System.out.println();
    if (!name.equals("stop"))
    {
        newDivision1.setDivName(name);  //set name of object instance to input name.
        //prompt for employees
        System.out.println("Please input the number of employees for " + newDivision1.getDivName() + ".");
        int employees = input.nextInt(); //capture int value of employees
        System.out.println();
        if (employees < 0)
        {
            do {
                System.out.printf("Please input a positive value for the number of employees:\n");
                employees = input.nextInt();
            } while (employees < 0);
        }
        newDivision1.setDivEmployees(employees);    //set employees to object instance

        //prompt for average salary
        System.out.println("Please input the average salary for " + newDivision1.getDivName() + ". \nPlease enter as digits and decimal only, \nexclude \"$\" and comma's.\n");
        double salary = input.nextFloat(); //capture average salary as float
        if (salary < 0)
        {
            do {
                System.out.printf("Please input a positive value for the average salary: \n");
                salary = input.nextFloat();
            } while (salary < 0);
        }
        newDivision1.setAvgSalary(salary);//set average salary to object instance

        //output totals
        System.out.printf("The %s division contains %d employees with an average salary of $%.2f.\n", newDivision1.getDivName(), newDivision1.getDivEmployees(), newDivision1.getAvgSalary());
        System.out.printf("The total payroll for the division is $%.2f.\n", newDivision1.getTotalSalary()); 
    }
} while (!name.equals("stop"));

    System.out.println( "The Program will now Exit.\n");

    }//end main


}//end payroll

formatting inst quite right, but that's the basic program.

Any help would be greatly appreciated!

ADDED: As per the comment, I created 3 different scanners for inputting floats, strings, and ints all separately and it solved the problem perfectly!

Lesson learned, THANK YOU!!

  • possible duplicate of [for loop in Java runs 3 times before taking next input](http://stackoverflow.com/questions/23720118/for-loop-in-java-runs-3-times-before-taking-next-input) – awksp Jun 09 '14 at 03:28
  • It isn't strictly a syntactic error on your part. I'd just advise not to mix `nextLine()` and `nextInt/Float/Double/etc.()` calls because it leads to situations exactly like this, where you have whitespace sitting in the input stream that `nextLine()` snaps up the next time you call it – awksp Jun 09 '14 at 03:29

3 Answers3

0

Try to replace you nextLine() with next()..

Datz Me
  • 945
  • 1
  • 10
  • 27
0

The easiest solution I see, is something like this -

while (employees < 0) {
  System.out.printf("Please input a positive value for the number of employees:\n");
  if (input.hasNextInt()) {
    employees = input.nextInt();
  } else if (input.hasNext()) {
    input.next();
  } else {
    System.err.println("no input");
    System.exit(1);
  }
}

Or, you could read the next line and try it parse it -

while (employees < 0) {
  System.out.printf("Please input a positive value for the number of employees:\n");
  if (input.hasNextLine()) {
    employees = Integer.parseInt(input.nextLine().trim());
  } else {
    System.err.println("no input");
    System.exit(1);
  }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

The problem is that on the last call in the while loop you're using nextFloat() and since the user enters, for example, 5.0 and then presses the "Enter" key - the scanner grabs only the Float part but leaves the \n in the buffer.

When you go back to the beginning of the loop, nextLine() detects that \n in the buffer and continues (since it just read a "line").

You can work around it by adding another call to nextLine() at the end of the while-loop or by using next() instead of nextLine().

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129