0

I'm new to Java and i have been writing a program to exercise. Bellow is the problematic class of the program. I'm trying to manipulate float variables via the array, but i cant seem to affect them that way (e.g. array[i] = 1) nor can i get their values(always 0.0), but accessing them directly (e.g. variable = 1) works. Can anyone tell me what am I doing wrong? Thank you.

public class Statistics {
    private float switchWin, switchLose, stayWin, stayLose, gamesPlayed, switchWinP, switchLoseP, stayWinP, stayLoseP;
    private float statisticsArray[] = {switchWin, switchLose, stayWin, stayLose, gamesPlayed, switchWinP, switchLoseP, stayWinP, stayLoseP};

    public void setSwitchWin() {
        switchWin++;
    }

    public void setSwitchLose() {
        switchLose++;
    }

    public void setStayWin() {
        stayWin++;
    }

    public void setStayLose() {
        stayLose++;
    }

    public void setGamesPlayed() {
        gamesPlayed++;
    }

    public String getSwitchWinPercentage() {
        return Float.toString(switchWinP = (switchWin/gamesPlayed)*100);
    }

    public String getSwitchLosePercentage() {
        return Float.toString(switchLoseP = (switchLose/gamesPlayed)*100);
    }

    public String getStayWinPercentage() {
        return Float.toString(stayWinP = (stayWin/gamesPlayed)*100);
    }

    public String getStayLosePercentage() {
        return Float.toString(stayLoseP = (stayLose/gamesPlayed)*100);
    }

    public String getGamesPlayed() {
        return Integer.toString((int) gamesPlayed);
    }

    public void reset() {
        for(int i=0; i<statisticsArray.length; i++) {
            System.out.println(statisticsArray[i]);
            statisticsArray[i]=0.0f;
            System.out.println(statisticsArray[i]);
        }
    }
}
1
  • U should set the value for statisticsArray in all setter too. Commented Aug 22, 2013 at 2:29

5 Answers 5

2

In Java, primitives (float, int etc.) cannot be referenced like objects and do not pass by reference.

statisticsArray does not hold a reference to the variables you initialized it with, it creates a copy of the variables inside the array when you perform this call.

private float statisticsArray[] = {switchWin, switchLose, stayWin, [..]};

statisticsArray now holds the default values of the switchWin etc. variables (0).

Sign up to request clarification or add additional context in comments.

Comments

1

After you store the variables in the array, the array contains variables of its own. Therefore, when you want to change them later you cannot change the original variable, you need to change that variable in the array.

Like this:

public void setSwitchWin() {
    statisticsArray[0]++;
}

public void setSwitchLose() {
    statisticsArray[1]++;
}

public void setStayWin() {
    statisticsArray[2]++;
}

public void setStayLose() {
    statisticsArray[3]++;
}

public void setGamesPlayed() {
    statisticsArray[4]++;
}

.

private float switchWin, switchLose, stayWin, stayLose, gamesPlayed, switchWinP, switchLoseP, stayWinP, stayLoseP;
private float statisticsArray[] = {switchWin, switchLose, stayWin, stayLose, gamesPlayed, switchWinP, switchLoseP, stayWinP, stayLoseP};

Doing this is no point, it is the same as putting:

private float statisticsArray[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

Comments

0

The only place you are setting values in statisticsArray in the code you posted is in the constructor (which sets the values of switchWin, switchLose, stayWin, stayLose, ... to zero) and reset(), and which is just setting the values to zero again with statisticsArray[i]=0.0f;

In Java, the primitive floats are stored in the array, and their values are stored in the array by reference, so the array values are not changed when you change the value of the reference. That is, setting the value of switchWin=5.0 after you've stored switchWin in the array is not going to change the value of statisticsArray[0] to 5.0.

If you need to update your array values you could:

  • Add a method syncArray() that syncs the values in the array by calling statisticsArray[] = {switchWin, switchLose, stayWin, stayLose, gamesPlayed, switchWinP, ... and basically reconstructs the array again with the updated methods. Not a good solution
  • Replace all the ints with an object type like AtomicInteger,as another answerer suggested.
  • The simplest approach in my opinion is to access the array directly for all gets/sets using the array index (so use statisticsArray[0] instead of switchWin

3 Comments

I am increasing the values of variables with setter methods from other classes, and reset() is used to revert the values back to zero. Via setter methods values increase and return fine, but statisticsArray[] always returns zeros and doesn't seem to affect variables at all.
When you call the setters, the values are updated, but since Java primitives are not accessed via reference, it doesn't update the values stored in statisticsArray[]
That explains it. Thank you.
0

The float in java is not an object here. so, when you try to increment a float value, it doesn't change the float it self, that's means:

switchWin++

will not change switchWin it self, this operation just give another float.

these code will explain your question:

public static void main(String[] args) {
    int i = 1;
    int j = 0;
    j = i;
    i++;
    System.out.println(j);
    System.out.println(i);
}

you can use AtomicInteger to store your score, and there is a function to increment it, something like:

public static void main(String[] args) {
    AtomicInteger ai = new AtomicInteger(0);
    AtomicInteger[] array = { ai };
    System.out.println(array[0].get());
    ai.incrementAndGet();
    System.out.println(array[0].get());
}

Comments

0

I suggest using an ArrayList. Arrays are fine, but arraylist are Array on Roids.

  public class stats{

  ArrayList<Float> percent = new ArrayList();
  // list the floats

  public void do(){
  for(int i = 0; i < numberofFloats; i++){
       percent.add(float);
   }
  }

  public void reset(){
     percent.clear();
  }

  }

remember, you must re-add the values after the reset. For more info.

Visit the Java API

2 Comments

Yes, i tried with ArrayList but i had an "unexpected value" problem (if i recall correct).
Did you try initializing each float before you added it to the array, otherwise by default it is null and a Float Array only accepts floats not null. In other words declare, switchWin = o.of; switchLost = 0.0f; etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.