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 5 of 5

Thread: having issues with if else statement

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

    Default having issues with if else statement

    This is a payroll assignment, and I don't know exactly why my if and else statements keep returning errors, I've tried adding curly braces inside of the if and the else parts, yet can't seem to get it right. Could anyone please give me insight on what I am doing wrong? If there's anything else you see wrong, please give me your feedback but I do have to meet certain specifications assigned by the teacher, thank you.
    package assignmentproject1;
    /* CHANGE (FINAL) VAR NAMES, SCAN & VERIFY, 
    ORGANIZE AND PUT FORMULAS ON BOTTOM, ORGANIZE VAR @ TOP, */
    import java.util.*;
    public class ProjectTest 
    {
     
        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.printf ("Rate:%38.2f \n",rate);
     
                System.out.printf ("Hours:%37.1f \n",hours);
     
     
                //CHECKS CONDITION
                System.out.println ();// LINE SPACER
     
                if (hours > OVERTIMEHOURS)
     
                    overtime = true;
     
                else
                    overtime= false;
     
                    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;
     
                if (overtime) 
                    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.println ();
     
                System.out.print ("Enter hourly rate in dollars and cents --> ");
                rate =  stdin.nextDouble ();    
     
            }//end of while loop
     
        }//end main
     
    }//end class


  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: having issues with if else statement

    keep returning errors
    is there any error messages? please paste it here.

    you have a statement:
                if (hours > OVERTIMEHOURS)
     
                    overtime = true;
     
                else
                    overtime= false;
     
                    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;


    are the statements after overtime= false; must lie within else condition?
    if so, you must put curly braces that enclose all those statements.

    In if-else statements, and for loops also,
    you don't have to put curly braces if and only, each conditional
    statements has only one statement under it, otherwise, you need to put curly
    braces. because if not, only the first statement will be recognized under
    the said conditional statement. and you might end up with compile errors when
    you not properly enclose it



    --updated--
    I checked it.
    you have this on your code:
                if (overtime) 
                    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);

    if you want all statements lies within if (overtime) ,
    enclose all those statements with curly braces.
    and make sure that there is no statements outside if if
    you used else
    sorry for my english
    Last edited by dicdic; March 3rd, 2014 at 09:40 PM.

  3. The Following 2 Users Say Thank You to dicdic For This Useful Post:

    ggx7 (March 3rd, 2014), GregBrannon (March 3rd, 2014)

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

    Default Re: having issues with if else statement

    Hello! Thank you for answering
    well so do you mean like this?
    if (overtime)
    {
    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);
    }

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

    Default Re: having issues with if else statement

    Yep, are there still errors?

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

    ggx7 (March 4th, 2014)

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

    Default Re: having issues with if else statement

    Ah, sorry for the late response, everything runs great, the booleans checkout and the statements match thank you again!

    Sorry, feel like you deserve to see the code, I was having trouble locating it, I'm going to bed and thank you again for the help!
    package assignmentproject1;
    /* CHANGE (FINAL) VAR NAMES, SCAN & VERIFY, 
    ORGANIZE AND PUT FORMULAS ON BOTTOM, ORGANIZE VAR @ TOP, */
    import java.util.*;
    public class ProjectTest 
    {
     
        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.printf ("Rate:%38.2f \n",rate);
     
                System.out.printf ("Hours:%37.1f \n",hours);
     
     
                //CHECKS CONDITION
                System.out.println ();// LINE SPACER
     
                if (hours > OVERTIMEHOURS)
                {    
                    overtime = true;
                }
                else
                {
                    overtime= false;
     
                }
     
                if (overtime)
                {
                    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;
                }
                    //calculations always go after loops and if else statements.
                    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;
     
                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.println ();
     
                System.out.print ("Enter hourly rate in dollars and cents --> ");
                rate =  stdin.nextDouble ();    
     
            }//end of while loop
     
        }//end main
     
    }//end class

Similar Threads

  1. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  4. Issues with IF else Statement
    By YogiB1810 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2012, 11:49 AM
  5. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM