0

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[]"*
}

}

8
  • Its private right??I think it shouldn't. Commented Jan 15, 2016 at 11:26
  • playerInv is private, yes. Commented Jan 15, 2016 at 11:27
  • 1
    What error/output are you getting? Are you really initializing your array to all "" or are they going to be null? You do not check your array length correctly (length() is not hte right way) and what if you do not have any blank entries in the array? Commented Jan 15, 2016 at 11:28
  • new String["Blood Essence]; I doubt this compiles. Commented Jan 15, 2016 at 11:29
  • When I try to store a value with setPlayerInv() I get "cannot resolve symbol setPlayerInv(). They are all set to "" at the moment, yes. It's an inventory system so they need to all the empty when the program starts. If there are no blank entries, the string won't be stored in the array as it is a fixed size. Commented Jan 15, 2016 at 11:30

3 Answers 3

1

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

8 Comments

Thanks, I'll try this. Should I delete the other Getter and Setter for playerInv in this case?
No keep other methods as it is. Just add these two methods and change the for loop.
== ""? Really? And if it is null, this check will throw a NPE because you try to compare to "" first ...
Ooopss my bad. Just copied it from question.
This solution is giving me "Cannot resolve method getPlayerInv" and "cannot resolve method setPlayerInv(java.lang.string, int)"
|
0

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.

Comments

0

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

I did

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

and works fine.

2 Comments

Yeah, I have an instance of Player. I didn't add it here because I didn't think it would be relevant :/ I have Player thePlayer = newPlayer(); in a different method which is called in the OnCreate();
I got it , plz let me know , if you dont find a solution ,I'll keep an eye in about 3 hours.Thank you.

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.