Jay Roberts
Jay Roberts

Reputation: 63

How to change the value of a String Array from a different class?

This may be a very simple question, but I can't seem to find a suitable answer on Google. I have a class called Player, which has a String array called playerInv with a size of 10.

In my Main Activity Class, I want to run a for loop to determine the first index in the array that is empty (""). I then want to populate that with a new string, and then terminate the loop. How do I do this?

Sorry for the nooby question. Like I said, I've tried Google to no avail!

For Loop:

    String playerInvTemp[] = thePlayer.getPlayerInv; ERROR -- cannot resolve getPlayerInv
                for (int i=0; i < playerInvTemp.length; i++)
                {
                    if ((!playerInvTemp[i].isEmpty()) || playerInvTemp[i] == null)
                    {
                        setPlayerInv("Blood Essence", i); ERROR cannot resolve setPlayerInv
                        //invText.setText();
                        Blood = true;
                        break;
                    }
                }

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;
}
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 getPlayerInv(int pos)
{
    return playerInv[pos];
}

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

public void setPlayerInv(String val, int index)
{
    this.playerInv[index] = val;
}

public String getPlayerInv()
{
    return this.playerInv;  *//this gives error "Incompatible types. Required java.lang.string, found java.lang.string[]"*
}

}

Upvotes: 0

Views: 63

Answers (3)

Rohit5k2
Rohit5k2

Reputation: 18112

Do this

Add these two method in Player class

public void setPlayerInv(String val, int index) 
{
    this.playerInv[index] = val;
}

public String[] getPlayerInv()
{
    return this.playerInv;
}

then change your for loop like this

String playerInvTemp[] = thePlayer.getPlayerInv();
for (int i=0; i < playerInvTemp.length; i++)
{
    if (!playerInvTemp[i].isEmpty()) || playerInvTemp[i] == null)
    {
        setPlayerInv("Blood Essence", i);
        //invText.setText();
        Blood = true;
        break;
    }
}

Upvotes: 1

Tsakiroglou Fotis
Tsakiroglou Fotis

Reputation: 1031

Do you run an instance of constructor player()??

I did

Player a=new Player();
a.getPlayerInv(0)

and works fine.

Upvotes: 0

mikeb
mikeb

Reputation: 11267

Bunch of problems here, .length() is not valid for an array, it should be .length.

 `for (int i=0; i<thePlayer.getPlayerInv(i).length(); i++)`

You most likely mean null or at least need to check for it, here and you need [] not ():

if (thePlayer.getPlayerInv[i] == "" or theplayer.getPlayerInv[i] == null)

This is all wrong, and as a matter of fact you need to post your code and errors, you have many problems and should start with learning some basics about Java.

Try some beginners tutorials (https://www.google.com/search?q=java+tutorials&ie=utf-8&oe=utf-8). You have a lot of both syntax and logic errors.

Upvotes: 0

Related Questions