1

I'm using Eclipse on an a homework assignment and I'm really struggling. The goal is to write a payroll program that has a user enter their name, hours worked, pay rate, federal and state tax withheld, and then outputs the calculated information of their amounts withheld as well as their net pay.

I used what I was familiar with which is println statements to show the output but the teacher wants us to use the System.out.printf function and I can't get it to work at all. If I use println statements all the values fill but for some reason I can't manage to get the printf to do the same. What am I missing? If I use the printf function it gives me the error of

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method printf(Locale, String, Object[]) in the type PrintStream is not applicable for the arguments (String, String, String)
at Payrolls.main(Payrolls.java:31)"

Here's my code:

import java.util.Scanner;

public class Payrolls
{
   public static void main(String []args)
   {


   Scanner input = new Scanner(System.in);

   System.out.print("Enter employee first name: ");
   String employeeFirst = input.nextLine();

   System.out.print("Enter employee last name: ");
   String employeeLast = input.nextLine();

   System.out.print("Enter number of hours worked this week: ");
   int hoursWorked = input.nextInt();

   System.out.print("Enter hourly pay rate of the employee: ");
   double payRate = input.nextDouble();

   System.out.print("Enter federal tax withholding rate: ");
   double fedTax = input.nextDouble();

   System.out.print("Enter state tax withholding rate: ");
   double stateTax = input.nextDouble();

   System.out.println(" ");
   System.out.println(" ");
   //System.out.printf("%-20s %s\n", employeeFirst, employeeLast);
   System.out.println("Name: " + employeeFirst +" " +employeeLast);

   System.out.println("Hours Worked:" + hoursWorked);

   System.out.println("Pay Rate:" + payRate);



   double grossPay;
   grossPay = payRate * hoursWorked;

   System.out.println("Gross Pay:" + grossPay);

   double deductions;
   deductions = grossPay * fedTax;

   System.out.println("\tDeductions:");
   System.out.println("\t\tFederal Witholding %: $" +  deductions);

   double statTax;
   statTax = grossPay * stateTax;

   System.out.println("\t\tState Witholding %: $" +  statTax);

   double totalDeduction;
   totalDeduction = deductions + statTax;

   System.out.println("\t\tTotal Deduction: $" + totalDeduction);


   double netPay;
   netPay = grossPay - totalDeduction;

   System.out.println("Net Pay:" + netPay);
   }
}
Holloway
  • 6,412
  • 1
  • 26
  • 33
Carlos
  • 11
  • 2
  • 3
    Works fine for me (JDK8, IntelliJ, F24). What JDK and/or IDE are you using? – Mureinik Oct 18 '16 at 09:57
  • which version of java do you use ? – Ray Lloy Oct 18 '16 at 09:57
  • Look at [here](http://stackoverflow.com/questions/14008466/how-to-use-system-out-printf) – Karthik Oct 18 '16 at 09:59
  • As far as I know, I'm using the latest version. I'm using Eclipse as the teacher demands we use it. – Carlos Oct 18 '16 at 09:59
  • 1
    I managed to get the exact same error by using Java 1.4. Maybe you should check that you are not using this version. – Loïc Oct 18 '16 at 10:02
  • Also check this and make sure you were using the latest jdk version. http://stackoverflow.com/questions/12588537/how-to-change-jdk-version-for-an-eclipse-project – Unknown Oct 18 '16 at 10:05
  • could you please provide a sniped where you actually use printf instead of println since printf expects other paramters – Till Hemmerich Oct 18 '16 at 10:06

2 Answers2

0
  • Printf methode is available from java version 1.5. Make sure that your jdk version is greater than or equal to 1.5.
  • Your error tells you that the first argument of the printf method must be a Locale type see an exmaple here
Ray Lloy
  • 171
  • 12
  • But the first argument to `printf` does NOT have to be a `Locale`. There are two `printf` methods - one where the first argument is a `Locale` and another where it's a `String`. – Dawood ibn Kareem Oct 19 '16 at 07:42
0

Please ensure you're using a JDK 1.5 or above, as varargs and more specifically printf was added in that release. It is not sufficient to just compile with JDK 1.5, you'll also need to run your program using JRE 1.5+.

Additionally, see the Javadocs for printf: one requires a String as its first parameter, the other requires a Locale instance.

Calling it like the following:

System.out.printf("%s\n", employeeFirst, employeeLast);

should work (which is what you're commented code does).

Alex
  • 8,093
  • 6
  • 49
  • 79
  • Unless I'm crazy (Which is a possibility given how mad I've been driven by a simple statement not working) I'm using JDK 1.8 I know I downloaded eclipse and the JDK back in late august when the semester started. – Carlos Oct 18 '16 at 10:36
  • Check with `javac -version` and `java -version`. Confirm (if you're building & runing through eclipse) that its Compiler configuration is set to *build* and thus *target* JDK 8. – Alex Oct 18 '16 at 10:40
  • @atc I believe that last comment of yours is the correct answer. OP appears to be **compiling** with JDK 1.5 or better, but **running** with JDK 1.4. – Dawood ibn Kareem Oct 19 '16 at 07:44