Below is part of my code for a simple java game. I am trying to keep the most recent 10 scores and names of the game in the prevScore and prevScoreName arrays. Both return statements in the method come up as errors, saying it requires int[] and what is found is int. The first two lines below are in the main method of my code, calling up the method. I have no idea what to do, any help would be appreciated.

    prevScore = scoreChange (prevScore, score);
    prevScoreName = nameChange (prevScoreName, newName);

    public static int[] scoreChange (int prevScore[], int score)
    {
        for (i=1; i<prevScore.length;i++){
                prevScore[i] = score;
        System.out.println(prevScore[i]);}
    return prevScore[i];
    }

    public static String[] nameChange (String prevScoreName[], String newName)
    {
        for (i=1; i<prevScoreName.length;i++){
                prevScoreName[i] = newName;
        System.out.println(prevScoreName[i]);}
    return prevScoreName[i];
    }

I'm pretty sure that in Java, like C++, if you pass an array into a function it automatically passes by reference. This means that you do not have return the array to upadte it in main(), since every change done to the array is done where the array is actually stored in memory.

Long story short, just make two void functions and don't bother returning the arrays.

The reason why you are getting errors is because you are trying to return a single integer when in your function def you are saying it will return a pointer to an integer (unless Java can actually return arrays, in which case that would be an array). When you put prevScore[i] that means index i of the array prevScore, whereas if you put prevScore that would give a pointer to the first index of prevScore.

If I make the functions void, I can't call them in the main body since then that errors instead. Is there anyway to keep track of the scores in the array? If i take the [i] out of both return functions, it runs, but when I check the scoreboard no names or scores come up after a game is played.

When using void functions you don't try to assign them to anything.
Meaning don't do prevScore = scoreChange (prevScore, score);, you would just have to do scoreChange (prevScore, score);.

Since prevScore is being passed by reference (because it is an array) any changes done to it in the function will modify the array from where ever it was called. So after the function call prevScore should hold the value that you are setting all the elements equal to.

The reason why you are getting errors is because you are trying to return a single integer when in your function def you are saying it will return a pointer to an integer (unless Java can actually return arrays, in which case that would be an array).

This is very 99% correct. Java arrays (and all objects) are passed as parameters by passing a copy of a reference to the array or object, so any code that updates the array or object in the method is updating the original verison.
Any kind of array or object can be returned from a method.
ints, like booleans, floats, chars etc are different from arrays and objects. These are called primitives, and are treated as simple values rather than refernces. So if you pass an int to a method java passes a copy of the int, and any changes you make make do not affect the original version. Similarly, if you return an int you return the actual value, not a reference to it (that's the 1% that was wrong)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.