I need some Java Help

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
Kumba
DBB Ace
DBB Ace
Posts: 176
Joined: Sun Nov 21, 1999 3:01 am
Location: Florida
Contact:

I need some Java Help

Post 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); 
   } 
} 
User avatar
Verran
DBB Captain
DBB Captain
Posts: 589
Joined: Thu Nov 05, 1998 12:01 pm
Location: Colorado
Contact:

Post 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); 
   } 
}
Kumba
DBB Ace
DBB Ace
Posts: 176
Joined: Sun Nov 21, 1999 3:01 am
Location: Florida
Contact:

Post 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?
Kumba
DBB Ace
DBB Ace
Posts: 176
Joined: Sun Nov 21, 1999 3:01 am
Location: Florida
Contact:

Post by Kumba »

Nevermind. I fixed it! Thanks a ton :)
Post Reply