0

so this is the main code for my text-based game.

import java.util.Scanner;

public class D_M_RPG {
    public static void main(String[] args) {
        //Creating the class to call on my toolbox
        D_M_RPGtoolbox toolbox = new D_M_RPGtoolbox();

        //Creating the scanner class for user input
        Scanner input = new Scanner(System.in);

        //Initiating variables and final variables aswell as arrays
        //token variable to validate open spots in an array
        int slotCounter = 0;
        int inventoryExpander = 11;

        //First initiated will be the character creation variables
        String hairColor = "";
        String eyeColor = "";
        String skinColor = "";
        String gender = "";

        //Initiating the arrays for character inventory slots
        String[] weaponSlots = new String[10];

        //initiating the arrays for the character creation
        String[] hairColorARR = {"black","Green","Yellow","Brown","Blue","Blonde","Grey","White"};
        String[] eyeColorARR = {"Green","Brown","Blue","Grey",};
        String[] skinColorARR = {"White","brown","Black",};
        String[] genderARR = {"Male","Female"};

        //Creating the introduction title and introduction
        System.out.println("Welcome to, COLD OMEN.");
        System.out.println("\nNOVEMBER 12th, 2150: ONTARIO, CANADA");
        System.out.println("\nYou hear loud shouts and gun fire all around you but can't pinpoint the location of anything, you feel a bit dazed until someone grabs you and you open your eyes and snap out of it.");
        System.out.println("\nUnknown: 'Get up, its time to move out. Take this.'");
        System.out.println("\nUnknown hands you a 'M4-A4 RIFLE'");
        System.out.println("\nyou manage to catch a small glimpse of him before you get up.");

        //Character creation screen
        System.out.println();

        //ONLY WORKS ONCE WONT INCREMEMENT THE SLOTCOUNTER
        toolbox.insert(weaponSlots, slotCounter, inventoryExpander, "M4-A4 RIFLE");
        System.out.println("\n" + weaponSlots[0]);
        toolbox.insert(weaponSlots, slotCounter, inventoryExpander, "ak47");
        System.out.println(weaponSlots[0]);
    }
}

so I have this method I made to basically add an "item" to the weaponSlots array (the inventory) but whenever I run it it will add to the first element in the array [0] but it wont incremement the slotcounter which should go up by one every time the method is used so that I dont replace any items in the array It should just add items until its full which is checked using the inventoryExpander variable. at the moment I have it printing the element at 0 and 0 for the array but i have checked 1 aswell and 1 is just null no item added it only just replaces the element at 0. heres the code for the method to increment etc:

public class D_M_RPGtoolbox {

    //method for random number generating to be used for crit hits, turns, loot generation etc
    public int randomGen(){
        int x = (int) (Math.random()*((20-0)+1)+0);
        return x;
    }

    //method for inserting into an array ONLY WORKS ONCE WONT INCREMEMENT THE SLOTCOUNTER FIX 
    public void insert(String[] a, int b, int d , String c) {
        if(b < d) {
            a[b] = c;
            b++;
        }//end of if statement
    }//end of method
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Your condition `if (b < d)` is only evaluated once per call. Try using a loop to step through the array by repeatedly incrementing the index variable with `b++`. – hc_dev Dec 19 '19 at 22:06

2 Answers2

0

What you are actually performing the ++ operation on in b is a copy of the value in slotCounter.

The variable slotCounter is passed into insert "by-value". This unlike what you probably imagine, that it is passed "by-reference".

One solution would be to do the slotCounter++ from the call row instead; and another would be to let the toolbox own the slotCounter variable completely.

This question uses the image of passing a copy of document content (by value) where changes to the document would not be seen by the sender; or as a link to a shared document (by reference), where changes could be made to the same page that the sender sees.

O.O.
  • 828
  • 8
  • 17
0

Its always going to be zero since you are passing zero and incrementing the local variable b.

Try calling the method as below with post increment ++ to slotCounter and see if it works for you,

toolbox.insert(weaponSlots, slotCounter++, inventoryExpander, "M4-A4 RIFLE");
Shubham Saraswat
  • 559
  • 4
  • 11