Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Payroll assignment

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Payroll assignment

    Hi guys, I'm having some issues with this format specifier error, but I can't find the error. When I input integers, the code runs fine.
    In addition, when I input a number greater than 40 for the hours, it gets an error too .

    So I'm getting errors for when I input:
    [*]decimal values for either: hourly pay rate or hours worked
    and[*]if I input a number > 40 for the hours variable, it returns an error.

    PLEASE CLICK SPOILER below for the error:
    [spoiler]run-single:
    Enter hourly rate in dollars and cents --> 8
    Enter number of hours and tenths worked --> 45

    PAYROLL REPORT
    Rate: 8.00
    Hours: 45.00

    Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
    Gross Pay: at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:4045)
    at java.util.Formatter$FormatSpecifier.printFloat(For matter.java:2761)
    at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2708)
    at java.util.Formatter.format(Formatter.java:2488)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at assignmentproject1.Project1.main(Project1.java:66)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 8 seconds)[/spoiler]
    Please see my code listed below
    package assignmentproject1;
    /* CHANGE (FINAL) VAR NAMES, SCAN & VERIFY, 
    ORGANIZE AND PUT FORMULAS ON BOTTOM, ORGANIZE VAR @ TOP, */
    import java.util.*;
    public class Project1 
    {
     
        public static void main (String[] args) 
        {
     
            Scanner stdin = new Scanner (System.in);
     
     
     
            final double FEDTAX = 0.25, CALITAX = 0.09075, SS_MEDI_FICA = 0.0765,
                    UEIDI = 0.02, OVERTIMEHOURS; 
            double  fedtax, ss_medi_fica, grossPay, rate, totalcost_to_Employer, 
                    calitax, netPay, hours, ueidi, employerFICA;
            boolean overtime = false;
     
     
     
            //Inputs rate and hours, then calculates the gross pay.
            System.out.print ("Enter hourly rate in dollars and cents --> ");
            rate =  stdin.nextDouble ();
     
     
     
            while (rate > 0)
            {           
                System.out.print ("Enter number of hours and tenths worked --> ");
                hours = stdin.nextInt ();  
     
                System.out.println ();// LINE SPACER
     
                System.out.println ("               PAYROLL REPORT");
     
                System.out.printf ("Rate:%38.2f                            \n",rate);
     
                System.out.printf ("Hours:%37.2f                           \n",hours);
     
     
     
                grossPay = rate * hours; //calculates gross pay.
                fedtax = grossPay * FEDTAX; //calculates federal tax.       
                calitax = grossPay * CALITAX; //calculates state (CA) tax.
                ss_medi_fica = grossPay * SS_MEDI_FICA; //employee pays for FICA.
                ueidi = grossPay * UEIDI;        
                employerFICA = grossPay * SS_MEDI_FICA; //employer pays for FICA.            
                netPay = grossPay - (fedtax + CALITAX + SS_MEDI_FICA);
                totalcost_to_Employer = grossPay + employerFICA 
                    + UEIDI;
     
                //CHECKS CONDITION        
                System.out.println ();// LINE SPACER
     
                if (hours > 40)
                { 
                    overtime = true;
                    grossPay = rate * 40 + (hours - 40) * (rate * 1.5);
                    System.out.printf ("Gross Pay: %32.2f \n", grossPay   
                    + "includes overtime");
                }
                else
                {
                    grossPay = rate * hours;
                    System.out.printf ("Gross Pay: %32.2f \n", grossPay);
     
     
     
                System.out.printf ("Federal Tax: %30.2f \n", fedtax);
     
                System.out.printf ("State Tax: %32.2f \n", calitax);
     
                System.out.printf ("FICA: %37.2f \n", ss_medi_fica);
     
                System.out.printf ("Net Pay: %34.2f \n", netPay);
     
                System.out.println ();
     
                System.out.printf ("Employer's FICA contribution: %13.2f \n", 
                    employerFICA);
     
                System.out.printf ("Employer's UEI and DI contribution: %7.2f \n",
                    UEIDI);
     
                System.out.printf ("Cost to Employer: %25.2f \n", 
                    totalcost_to_Employer);                    
     
     
     
     
                }
     
                System.out.print ("Enter hourly rate in dollars and cents --> ");
                rate =  stdin.nextDouble ();    
     
                overtime = false;/*have to reset overtime, 
                                    so it doesn't apply to regular hours*/
            }
     
     
     
     
        }//end main
     
    }//end class
    Last edited by ggx7; February 26th, 2014 at 11:28 PM. Reason: forgot to add error, please see SPOILER. -errr guess it doesn't work, lol.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Payroll assignment

    So I'm getting errors for when I input:[*]decimal values for either: hourly pay rate or hours worked
    and[*]if I input a number > 40 for the hours variable, it returns an error.
    the error you encounter is because of this line:
    System.out.printf ("Gross Pay: %32.2f \n", grossPay
    + "includes overtime");


    when the hours > 40
    it will execute the following statement in your code
    if (hours > 40)
                { 
                    overtime = true;
                    grossPay = rate * 40 + (hours - 40) * (rate * 1.5);
                    System.out.printf ("Gross Pay: %32.2f \n", grossPay   
                    + "includes overtime");
                }

    but the way you used System.out.printf is invalid.
    if you want to concatenate "includes overtime" after grosspay
    it should be like this
    System.out.printf ("Gross Pay: %32.2f \n", grossPay);
    System.out.println(" includes overtime");
    (not sure if can do it in a single liner code)

    but if you the output to be 1 line like: Gross pay 123123 includes overtime
    try to used String.format

    you format gross pay wint %32.2f, i think it will also work with this %.2f .2f means to format decimal in 2 decimals places only
    Last edited by dicdic; February 27th, 2014 at 12:09 AM.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Payroll assignment

    Quote Originally Posted by dicdic View Post
    the error you encounter is because of this line:
    System.out.printf ("Gross Pay: %32.2f \n", grossPay
    + "includes overtime");


    when the hours > 40
    it will execute the following statement in your code
    if (hours > 40)
                { 
                    overtime = true;
                    grossPay = rate * 40 + (hours - 40) * (rate * 1.5);
                    System.out.printf ("Gross Pay: %32.2f \n", grossPay   
                    + "includes overtime");
                }

    but the way you used System.out.printf is invalid.
    if you want to concatenate "includes overtime" after grosspay
    it should be like this
    System.out.printf ("Gross Pay: %32.2f \n", grossPay);
    System.out.println(" includes overtime");
    (not sure if can do it in a single liner code)

    but if you the output to be 1 line like: Gross pay 123123 includes overtime
    try to used String.format

    you format gross pay wint %32.2f, i think it will also work with this %.2f .2f means to format decimal in 2 decimals places only
    Oh geez! Friggin A, can't believe I made a bonehead move like that >_< . Thanks alot! I think it works better now, here is my new code!
    package assignmentproject1;
    /* CHANGE (FINAL) VAR NAMES, SCAN & VERIFY, 
    ORGANIZE AND PUT FORMULAS ON BOTTOM, ORGANIZE VAR @ TOP, */
    import java.util.*;
    public class Project1 
    {
     
        public static void main (String[] args) 
        {
     
            Scanner stdin = new Scanner (System.in);
     
     
            final int OVERTIMEHOURS = 40;           
            final double FEDTAX = 0.25, CALITAX = 0.09075, SS_MEDI_FICA = 0.0765,
                    UEIDI = 0.02, OVERTIMEMULTIPLIER = 1.5;
            double  fedtax, ss_medi_fica, grossPay, rate, totalcost_to_Employer, 
                    calitax, netPay, hours, ueidi, employerFICA;
            boolean overtime = false;
     
     
     
            //Inputs rate and hours, then calculates the gross pay.
            System.out.print ("Enter hourly rate in dollars and cents --> ");
            rate =  stdin.nextDouble ();
     
     
     
            while (rate > 0)
            {           
                System.out.print ("Enter number of hours and tenths worked --> ");
                hours = stdin.nextDouble ();  
     
                System.out.println ();// LINE SPACER
     
                System.out.println ("               PAYROLL REPORT");
     
                System.out.printf ("Rate:%38.2f                            \n",rate);
     
                System.out.printf ("Hours:%37.2f                           \n",hours);
     
     
     
                grossPay = rate * hours; //calculates gross pay.
                fedtax = grossPay * FEDTAX; //calculates federal tax.       
                calitax = grossPay * CALITAX; //calculates state (CA) tax.
                ss_medi_fica = grossPay * SS_MEDI_FICA; //employee pays for FICA.
                ueidi = grossPay * UEIDI;        
                employerFICA = grossPay * SS_MEDI_FICA; //employer pays for FICA.            
                netPay = grossPay - (fedtax + CALITAX + SS_MEDI_FICA);
                totalcost_to_Employer = grossPay + employerFICA 
                    + UEIDI;
     
                //CHECKS CONDITION        
                System.out.println ();// LINE SPACER
     
                if (hours > 40)
                { 
                    overtime = true;
                    grossPay = rate * OVERTIMEHOURS + (hours - OVERTIMEHOURS) 
                            * (rate * OVERTIMEMULTIPLIER);
                    System.out.printf ("Gross Pay: %32.2f ", grossPay);   
                    System.out.print ("includes overtime\n");
                }
                else 
                {
                    grossPay = rate * hours;
                    System.out.printf ("Gross Pay: %32.2f \n", grossPay);
     
                }
                System.out.printf ("Federal Tax: %30.2f \n", fedtax);
     
                System.out.printf ("State Tax: %32.2f \n", calitax);
     
                System.out.printf ("FICA: %37.2f \n", ss_medi_fica);
     
                System.out.printf ("Net Pay: %34.2f \n", netPay);
     
                System.out.println ();
     
                System.out.printf ("Employer's FICA contribution: %13.2f \n", 
                    employerFICA);
     
                System.out.printf ("Employer's UEI and DI contribution: %7.2f \n",
                    UEIDI);
     
                System.out.printf ("Cost to Employer: %25.2f \n", 
                    totalcost_to_Employer);    
     
                System.out.print ("Enter hourly rate in dollars and cents --> ");
                rate =  stdin.nextDouble ();    
     
                overtime = false;/*have to reset overtime, 
                                    so it doesn't apply to regular hours*/
            }//end of while loop
     
     
     
     
        }//end main
     
    }//end class

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Payroll assignment

    great, glad to help you

  5. The Following User Says Thank You to dicdic For This Useful Post:

    ggx7 (February 27th, 2014)

Similar Threads

  1. Someone who can help me with my PAYROLL PROGRAM
    By driczdc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2013, 05:02 AM
  2. employe payroll
    By daim9996 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 18th, 2013, 08:35 AM
  3. [SOLVED] Please help me with this payroll program
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 06:47 PM
  4. heeeeeyyyy .. HELP ! :) -- PAYROLL SYSTEM --
    By princess in forum Java Theory & Questions
    Replies: 5
    Last Post: February 27th, 2011, 03:11 PM
  5. Payroll
    By Kesh486 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 6th, 2010, 06:48 PM