Writing my second program for a class. It requires the following:
Create a non-GUI-based Java application that calculates the payroll for a department in an organization. The application should display text that requests the user input for the name of the department, the number of employees in the department, and the average salary for the employees in the department. The number of employees in the department should be a whole number, and the average salary for the employees should be a real number. The application should then print out the name of the department and the total department payroll for all of the employees in the department (number of employees times the average salary for each employee). The total department payroll amount should be a real number. In the printout, display the dollar symbol ($) to the left of the total department payroll amount and format the amount to display currency.
Here is my attempt
// Payroll Program
import java.util.Scanner; // program uses class Scanner
public class PayrollProgram
{
//main method begins execution of the Java application
public static void main( String[] args )
{
//create a Scanner to obtain input from the user in the command window
Scanner input = new Scanner( System.in );
// My variables
String departName; // Variable for Department Name
int employees; // Variable for the number of employees
float avgSalary; // Variable for average salary
float totSalary; // Weekly pay total (employees * avgSalary = totSalary)
System.out.println( "Welcome to the Payroll Program\nWe are here to serve you" );
System.out.println( ); // One blank line
System.out.print ( "Please enter the name of the Department: " ); // First input is name of Department
departName = input.nextLine();
System.out.print ( "Enter number of employees: " ); // Second input for number of workers
employees = input.nextInt();
System.out.print ( "Enter the average weekly salary of each employee: " ); // Third Input for average salary
avgSalary = input.nextFloat();
totSalary = (float) employees * avgSalary; // Calculate Department's weekly payroll
//Output results
System.out.printf( "For the Department %s\n", departName );
System.out.printf( "With this number of employees %d\n ", employees );
System.out.printf( "And with this average salary: %.2f\n " , avgSalary );
System.out.printf( "The weekly total payroll is $%.2f\n", totSalary );
System.out.println( "Thank you, and Goodbye" );
} // end method main
} // end class PayrollProgram
When I run it, it's all great until it prompts for the average salary, if I enter a number, then I get this message in the command window:
For the Department Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2661)
at java.util.Formatter.format(Formatter.java:2433)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at PayrollProgram.main(PayrollProgram.java:35)
All I need is a hint to get me going in the right direction. I've just lost four hours trying to figure this out. :(