Jay Roberts
Jay Roberts

Reputation: 63

Trying to create a new object from a class using a string array

I have a class called Player, in which I have three variables, one of which is a string array (playerInv) with ten values. How do I create a new Player in my main activity class using this Player class, with each index in the array having a null value ("")?

The Player Class:

public class Player {

    private int playerPos;
    private int playerHP;
    private String playerInv[];

    Player(int startPos, int startHP, String[] newInventory[]) {
        playerPos = startPos;
        playerHP = startHP;
        playerInv = newInventory[10];
    }

    public int getPlayerPos() {
        return playerPos;
    }

    public void setPlayerPos(int playerPos) {
        this.playerPos = playerPos;
    }

    public int getPlayerHP() {
        return playerHP;
    }

    public void setPlayerHP(int playerHP) {
        this.playerHP = playerHP;
    }

    public String setPlayerInv(int pos) {
        return playerInv[pos];
    }

    public void setPlayerInv(String inventory) {
        for (int i = 0; i < 10; i++) {
            this.playerInv[i] = playerInv[i];
        }
    }
}

Initializing the Player in the Main Activity

public void setupPlayer()
{
    thePlayer = new Player(0,100,);
}

Thanks!

Upvotes: 1

Views: 67

Answers (3)

Rozart
Rozart

Reputation: 1778

For the beginning there is an error in your classes constructor. It should look like this if you want to get the String array as a constructor parameter:

Player(int startPos, int startHP, String[] newInventory)
{
    playerPos = startPos;
    playerHP = startHP;
    playerInv = newInventory;
}

Or it should look like this, if you want playerInv to be always an String array with size 10.

Player(int startPos, int startHP)
{
    playerPos = startPos;
    playerHP = startHP;
    playerInv = new String[10];
}

Then you create a new Player object with simple:

thePlayer = new Player(0, 100);

What is important, in the second case playerInv variable would contain reference to array of type String filled with null. So the table will look like this:

[null, null, null, null, null, null, null, null, null, null]

If you would like to have an array filled with empty String ("") values that looks like this:

   ["","","","","","","","","",""]

Then you also have at least 2 ways of doing this:

Player(int startPos, int startHP)
{
    playerPos = startPos;
    playerHP = startHP;
    playerInv = new String[10];
    Arrays.fill(playerInv, "");
}

Or you can use this constructor:

Player(int startPos, int startHP, String[] newInventory)
{
    playerPos = startPos;
    playerHP = startHP;
    playerInv = newInventory;
}

And do this when creating a new Player object:

String[] inventory = new String[10];
Arrays.fill(inventory, "");
Player thePlayer = new Player(0, 100, inventory);

Upvotes: 2

Use a constructor that makes what you want by default.

Upvotes: 0

Laerte
Laerte

Reputation: 7083

In your Player constructor:

Player(int startPos, int startHP, String[] newInventory)
{
    playerPos = startPos;
    playerHP = startHP;

    if (newInventory == null)
    {
        playerInv = new String[10];
        for (int i = 0; i < playerInv.length; i++)
        {
            playerInv[i] = "";
        }
    }
    else
    {
        playerInv = newInventory;
    }
}

Then call:

thePlayer = new Player(0,100,null);

Hope it helps...

Upvotes: 0

Related Questions