-1

I am having trouble figuring out why the output is all 0 instead of the respective user input. I have set every variable to static so it can be used everywhere. What I am confused about is the random 0 that comes out of nowhere.

static int hours,payrate,id;
static double grosspay;
static Scanner sc = new Scanner(System.in);
public static void readInput(int hours,int payrate,int id,Scanner sc)
{
    if (id != -1)
    {
        System.out.println("Employees id : ");
        id = sc.nextInt();
        System.out.println("The number of hours worked is : ");
        hours = sc.nextInt();
        System.out.println("The standard payrate is : ");
        payrate = sc.nextInt();
    }
    else
        return;
}

public static double computePay(int hours,double grosspay,int payrate)
{

    if(hours <= 160)
        grosspay = hours * payrate;
    else
        grosspay = 160 * payrate + (hours - 160) * 1.5 * payrate;
    return grosspay;
}

public static void Output(double grosspay,int id)
{
    System.out.println("For Employee id " + id + ", grosspay is " + grosspay);
}


public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    readInput(hours,payrate,id,sc);
    System.out.println("Grosspay is: " + computePay(hours,grosspay,payrate));
    Output(grosspay,id);
    System.out.println(id);
raymondT
  • 83
  • 4
  • In your `readInput` method you're assigning the result to the variables passed to the method. Instance variables are *not* affected by this assignment. – Maroun Dec 07 '16 at 13:53
  • Where ? With what input ? Could you explain you test and problem a bit more please. – AxelH Dec 07 '16 at 13:54

3 Answers3

4

Your mistake is passing parameters to readInput. These parameters are local variables, which hide the static members having the same names. Therefore the static members are not updated.

Simply changing your method to :

public static void readInput() {
    if (id != -1) {
        System.out.println("Employees id : ");
        id = sc.nextInt();
        System.out.println("The number of hours worked is : ");
        hours = sc.nextInt();
        System.out.println("The standard payrate is : ");
        payrate = sc.nextInt();
    }
}

will solve this issue.

However, your code doesn't look good. You should prefer instance variables or local variables over static variables.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

Change your method to use this to refer to the actual static class variables and all will be fine:

public static void readInput(int hours,int payrate,int id,Scanner sc) {
    if (id != -1) {
        System.out.println("Employees id : ");
        this.id = sc.nextInt();
        System.out.println("The number of hours worked is : ");
        this.hours = sc.nextInt();
        System.out.println("The standard payrate is : ");
        this.payrate = sc.nextInt();
    }
    return;
}

Because the parameters in readInput() have the same names as the static class variables, the former variables "mask" the latter. Hence, your assignments fall on deaf ears and do not stick.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

In your readInput method you're assigning the result to the variables passed to the method. Instance variables are not affected by this assignment.

What I am confused about is the random 0 that comes out of nowhere.

It's not random at all, this behavior is stated in the JLS 4.12.5. Initial Values of Variables:

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):

  • [...]
  • For type int, the default value is zero, that is, 0.
  • [...]
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • ...but the variables he intends to assign in `readInput()` are not instance variables, they are static class level variables. But yeah I get the point you are trying to make. – Tim Biegeleisen Dec 07 '16 at 14:01
  • @TimBiegeleisen The class variables are static, and they have default values. – Maroun Dec 07 '16 at 14:03