Page 1 of 1

I need some Java Help

Posted: Sat Feb 11, 2006 10:31 am
by Kumba
Hey guys I am working on a program for school and I'm having some serious issues with it. Currently it is telling me I have not declared the variable 'rate' when I try to print out my results. Maybe you guys can take a look and tell me wtf I'm doing wrong. TIA for any assistance.

Code: Select all

import javax.swing.JOptionPane; 
public class Pay 
{ 
   public static void main(String[] args) 
   { 
      String jobTypeString, empHoursString; 
      int jobType; 
      double empHours; 
      double regularPay; 
      double rate; 
      double grossPay = 0.0; 
      double overtimePay = 0.0; 
      double maxHours = 50.0; 
      //Request employee type and hours worked from user 
      jobTypeString = JOptionPane.showInputDialog(null, 
      \"Please enter the job type.\" + 
      \"/nChoose type 1, 2, or 3\"); 
      jobType = Integer.parseInt(jobTypeString); 
      empHoursString = JOptionPane.showInputDialog(null, 
      \"Please enter the hours worked.\" + 
      \"/nHours are not to exceed 50.\"); 
      empHours = Integer.parseInt(empHoursString); 

          
      if(jobType == 1 && empHours <= 50 && empHours > 40) 
         { 
            rate = 7.00; 
            regularPay = 40 * rate; 
            overtimePay = (empHours - 40) * 2 * rate; 
            grossPay = regularPay + overtimePay; 
         } 
         else if(empHours < 40) 
         { 
            rate = 7.00; 
            regularPay = 40 * rate; 
            overtimePay = overtimePay; 
            grossPay = regularPay + overtimePay; 
         } 
      else if(jobType == 2 && empHours <= 50 && empHours > 40) 
         { 
            rate = 10.00; 
            regularPay = 40 * rate; 
            overtimePay = (empHours - 40) * 2 * rate; 
            grossPay = regularPay + overtimePay; 
         } 
         else if(empHours < 40) 
         { 
            rate = 10.00; 
            regularPay = 40 * rate; 
            overtimePay = overtimePay; 
            grossPay = regularPay + overtimePay; 
         } 
      else if(jobType == 3 && empHours <= 50 && empHours > 40) 
         { 
            rate = 12.00; 
            regularPay = 40 * rate; 
            overtimePay = (empHours - 40) * 2 * rate; 
            grossPay = regularPay + overtimePay; 
         } 
         else if(empHours < 40) 
         { 
            rate = 12.00; 
            regularPay = 40 * rate; 
            overtimePay = overtimePay; 
            grossPay = regularPay + overtimePay; 
         }    
      //Display hours worked, rate, regular pay, and overtime pay for employee. 
      JOptionPane.showMessageDialog(null, 
      \"The employee worked \" + empHours + \" at the rate of \" + rate + 
      \"dollars per hour.\" + 
      \"/nThe regular pay amount for the week is \" + regularPay + 
      \"/nThe overtime pay amount for the week is \" + overtimePay + 
      \"/nThe gross pay amount for the week is \" + grossPay); 
      System.exit(0); 
   } 
} 

Posted: Sat Feb 11, 2006 12:08 pm
by Verran
Some of your member variables needed to be given a default value. I also fixed /n to \\n and made the messages a little prettier.

Code: Select all

import javax.swing.JOptionPane; 
public class Pay 
{ 
   public static void main(String[] args) 
   { 
      String jobTypeString, empHoursString; 
      int jobType; 
      double empHours = 0.0; 
      double regularPay = 0.0; 
      double rate = 0.0; 
      double grossPay = 0.0; 
      double overtimePay = 0.0; 
      double maxHours = 50.0; 
      //Request employee type and hours worked from user 
      jobTypeString = JOptionPane.showInputDialog(null, 
      \"Please enter the job type. \" + 
      \"\\nChoose type 1, 2, or 3\"); 
      jobType = Integer.parseInt(jobTypeString); 
      empHoursString = JOptionPane.showInputDialog(null, 
      \"Please enter the hours worked. \" + 
      \"\\nHours are not to exceed 50.\"); 
      empHours = Integer.parseInt(empHoursString); 

          
      if(jobType == 1 && empHours <= 50 && empHours > 40) 
         { 
            rate = 7.00; 
            regularPay = 40 * rate; 
            overtimePay = (empHours - 40) * 2 * rate; 
            grossPay = regularPay + overtimePay; 
         } 
         else if(empHours < 40) 
         { 
            rate = 7.00; 
            regularPay = 40 * rate; 
            overtimePay = overtimePay; 
            grossPay = regularPay + overtimePay; 
         } 
      else if(jobType == 2 && empHours <= 50 && empHours > 40) 
         { 
            rate = 10.00; 
            regularPay = 40 * rate; 
            overtimePay = (empHours - 40) * 2 * rate; 
            grossPay = regularPay + overtimePay; 
         } 
         else if(empHours < 40) 
         { 
            rate = 10.00; 
            regularPay = 40 * rate; 
            overtimePay = overtimePay; 
            grossPay = regularPay + overtimePay; 
         } 
      else if(jobType == 3 && empHours <= 50 && empHours > 40) 
         { 
            rate = 12.00; 
            regularPay = 40 * rate; 
            overtimePay = (empHours - 40) * 2 * rate; 
            grossPay = regularPay + overtimePay; 
         } 
         else if(empHours < 40) 
         { 
            rate = 12.00; 
            regularPay = 40 * rate; 
            overtimePay = overtimePay; 
            grossPay = regularPay + overtimePay; 
         }    
      //Display hours worked, rate, regular pay, and overtime pay for employee. 
      JOptionPane.showMessageDialog(null, 
      \"The employee worked \" + empHours + \" at the rate of \" + rate + 
      \"dollars per hour.\" + 
      \"\\nThe regular pay amount for the week is \" + regularPay + 
      \"\\nThe overtime pay amount for the week is \" + overtimePay + 
      \"\\nThe gross pay amount for the week is \" + grossPay); 
      System.exit(0); 
   } 
}

Posted: Sat Feb 11, 2006 12:44 pm
by Kumba
Thanks for the help. However I am noticing now that when I display the results it is outputting 0.0 as the result for everything except the hours worked. What would u suggest for fixing this problem?

Posted: Sat Feb 11, 2006 12:59 pm
by Kumba
Nevermind. I fixed it! Thanks a ton :)