1

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!

1
  • Do you want to have a null values or "" (empty Strings) in your array? Commented Jan 14, 2016 at 18:24

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Done! :) Another problem I'm having is that my thePlayer.getPlayerInv and thePlayer.setPlayerInv are coming up as "cannot resolve symbol" in my main activity. Any ideas?
You don't have a method getPlayerInv() in your Player class. I suggest using IntelliJ, Eclipe or Netbeans IDE when writing code in java, because they will give you error suggestions on the fly. :)
I have since corrected this. However, I still get "cannot resolve symbol" on both Get and Set methods :/
I'm sorry, but to answer this question I would need to see whole your code. Most possibly any IDE will help you fix your code. :)
0

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...

Comments

0

Use a constructor that makes what you want by default.

Comments

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.