Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: Wage Calculaor Total Pay is always 0. Help please?

  1. #1
    nathanskates is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Default Wage Calculaor Total Pay is always 0. Help please?

    Hello all, first post here. For my high school CS class we had to make a wage calculator using methods, but every time I run it, it returns the totalPay as 0.0. What's going wrong?
    Thanks in advance!
    Java Code:
    import java.util.Scanner;
    import java.lang.*;
    
    
    public class Wage_Calculator {
    
        private static int employeeID;
        private static double hourlyWage;
        private static double regularHours;
        private static double overtimeHours;
        private static double totalPay;
        private static String name;
    
    public static void main(String[] args) {
    
    employeeName();
    inputEmployeeID();
    inputHourlyWage();
    inputRegHours();
    inputOverHours();
    calcTotalPay();
    
                }
    
    public static void employeeName()    {
    //Enter employee name
            System.out.println("Please enter your full name: ");
            Scanner scan4 = new Scanner(System.in);
            name = scan4.nextLine();
    }
    public static void inputEmployeeID() {
    //Input Employee ID
            System.out.println("Please enter your employee ID: ");
            Scanner scan = new Scanner(System.in);
            int employeeID = scan.nextInt();
      }
    
    public static void inputHourlyWage() {
    //Enter in the hourly wage
            System.out.println("What is your hourly wage? ");
            Scanner scan1 = new Scanner(System.in);
            double hourlyWage = scan1.nextDouble();
      }
    
    public static void inputRegHours() {
    //Enter how many regular hours were worked
            System.out.println("Enter number of regular hours worked: ");
            Scanner scan2 = new Scanner(System.in);
            double regularHours = scan2.nextDouble();
      }
    
    public static void inputOverHours() {
    //Enter how many overtime hours were worked
            System.out.println("Enter number of overtime hours worked: ");
            Scanner scan3 = new Scanner(System.in);
            double overtimeHours = scan3.nextDouble();
      }
    
    public static void calcTotalPay() {
    //The total pay comes out to:
            totalPay = (regularHours * hourlyWage) + (overtimeHours * (hourlyWage * 1.5));
            System.out.println(name + ", your total pay for this week is: $" + totalPay);
      }
    
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Wage Calculaor Total Pay is always 0. Help please?

    You're hiding you class fields by redeclaring them in your methods. So don't redeclare them.

    And on an unrelated note: Don't declare the fields static. And you only need one scanner instance.

    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Wage Calculaor Total Pay is always 0. Help please?

    As per jim829's explanation:

    Quote Originally Posted by nathanskates View Post
    Java Code:
    public class Wage_Calculator {
    
        private static double hourlyWage;   // you are not updating THIS hourlyWage variable...
    
    public static void inputHourlyWage() {
    //Enter in the hourly wage
            System.out.println("What is your hourly wage? ");
            Scanner scan1 = new Scanner(System.in);
            double hourlyWage = scan1.nextDouble();  // ... but you are updating THIS hourlyWage variable
      }
    
    }
    Seems like a bit of copy/paste mistakery here ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Wage Calculaor Total Pay is always 0. Help please?

    Quote Originally Posted by jim829 View Post
    And on an unrelated note: Don't declare the fields static.
    Since every method is static then the fields need to be.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Wage Calculaor Total Pay is always 0. Help please?

    Yeah. I didn't notice that. @OP don't make you methods static either. :)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Wage Calculaor Total Pay is always 0. Help please?

    @OP don't make you methods static either. :)

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Wage Calculaor Total Pay is always 0. Help please?

    Quote Originally Posted by jim829 View Post
    don't make you methods static either.
    Cue whine about getting "Cannot make a non-static reference from a static context" error. (or whatever the wording is)

Similar Threads

  1. min, max, total using arrays
    By Andie in forum New To Java
    Replies: 1
    Last Post: 03-15-2012, 08:35 AM
  2. Calculating Total?
    By ethemartian in forum New To Java
    Replies: 6
    Last Post: 02-21-2011, 05:09 AM
  3. Printing total out
    By denisdoherty in forum New To Java
    Replies: 1
    Last Post: 04-25-2008, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •