2

Ok I'm really new to this and am not even sure I'm asking the right question but I want to create an object that has some regualr values like strings and ints etc. But in addition I want one of the properties to be an array of strings like this:

int hp =100;
int level =1;
int exp=0;
String[] items = {"hpPot","blank","blank","blank","blank"};
return new player(level, exp, hp, name, items[] );
}

This doesn't work and at the least I'd like to know exactly why.

Thanks.

5
  • Can you post your whole class player..? Commented Nov 22, 2011 at 6:56
  • Sidenote: why do you add "blank" to the items? Unless there's an actual item called "blank", I suggest you don't do this. Either pass in null for "empty" slots or (probably better) simply pass in an array of just a single element. The actual allocation of storage and checking of the maximum number of items should probably belong to the player class (which should be named ´Player` in accordance to the generally accepted Java Naming Conventions). Commented Nov 22, 2011 at 7:06
  • Like I said I'm very new blank was merely going to be an object without any values. ty. Commented Nov 22, 2011 at 7:11
  • @Joachim Sauer about the naming convention I usally keep the first word lowercase and then do CamelCase from there is that incorrect? Like "variableName" or "variable" or "variableNameHere" Commented Nov 22, 2011 at 7:26
  • @Suavelizard: it's correct for variables it's wrong for class names: It should be Player player = new Player(). Commented Nov 22, 2011 at 7:32

1 Answer 1

6

while passing array don't use subscript operator

return new player(level, exp, hp, name, items); // [] is removed.
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this before but it didn't work. I have tweaked some parts of the player class since and now I dont seem to have a problem thanks so much :P
@TheEliteGentleman: That's what keeps me encouraged to refer to stackoverflow. The appreciation you get in return is HUUUUGE.
well when you're banging your head against the wall you are grateful for people who give you a helmet, or move you to a padded room :P

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.