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

It doesn't show any of those errors when I try to compile it in netbeans 7.1?

maybe you could try printing it like this

System.out.println( "For the Department " + departName );
System.out.println( "With this number of employees " + employees );
System.out.println( "And with this average salary: " + avgSalary );
System.out.println( "The weekly total payroll is " + totSalary );

Edited 4 Years Ago by zeroliken: n/a

YOU ARE A PEACH! TO YOU GOES MUCH GOOD KARMA!! :)

I know, I used Netbeans as well and didn't see anything. :( But something in line 33 was creating a problem as once i took it out, re-recompiled it and ran it, it worked great.

But ah, your suggestions helps a lot! As I didn't know how to do that. Will using the "+" designator get me the rounding to the 100th decimal like %d\n does?

One more quick question:

What would the code look like to say

For the "departName" Department?

does this work?

System.out.println( "For the " + departName " Department " );

add one more + sign after the variable

System.out.println( "For the " + departName + " Department " );

My advice is don't make Java act like C :)

Edited 4 Years Ago by zeroliken: n/a

perfect. that is exactly what i needed to do. you are really are awesome! ;)

This question has already been answered. Start a new discussion instead.