• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting specific values from an array.  RSS feed

 
Xavier Neves
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. This is my first post on here, and I am teaching myself java despite me starting to take a class in school this year, because I am eager to jump right in. I have taught myself the basics (Or so I like to think.), but there are still some things I don't understand (Specifically getting certain pieces of information from an array, and also calling(?) things from other files in the same package.

I have this code here for creating monsters.

This array is made from the monster template code I have here.


I want to set a String variable (specifically a random name of the monster.) in my main game code. I cannot figure out how to get just the name part out of the monster from the array.
My end goal is to get a random monster to show, and then tell the player the name of the monster, and allow the player to fight it, but I need to know how to get the information on the monster out of the array to use in the code I have for fighting/taking damage and such.

If you need any more information or anything (I probably messed up in my explanation), then please tell me .
Thank you for taking the time to read this.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I cannot figure out how to get just the name part out of the monster from the array.

It is an array of Monsters. So first you will have to retrieve a monster from the array. Once you have the monster you can retrieve the monster name.
For selecting monsters randomly from the array check out the java.util.Random#next(int bits);
Note: Array index starts with zero and not 1!

A minor nitpick: Class names should start with a capital letter. So it should be Monster and not monster.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Xavier. Although it looks like you're working on a game, I've moved this thread to "Beginning Java," where I hope you'll get more attention.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you create an instance of class Monsters (note the upper case letter, as Maneesh Godbole suggested). Let's call it "myMonsters".
Then myMonsters.monsters is the array of monsters, and myMonsters.monsters[2].getMonsterName() will be the String "Py".

Suggestion: make the monsters variable private (or at least protected), and add a method to the Monsters class to retrieve one of the monsters.
For example,

would return the MonsterTemplate at the specified index. Then you would use myMonsters.getMonster(2).getMonsterName().
You could also have a method to retrieve it by its name:

which would search for the monster with the specified name, and return that instance (or null if it is not found)
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
You realise that you are using an array initialiser and initialising the array immediately, so you can omit new monsterTemplate[] in line 13. I think the monsterTemplate class might better be called Monster.
 
Xavier Neves
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:First, you create an instance of class Monsters (note the upper case letter, as Maneesh Godbole suggested). Let's call it "myMonsters".
Then myMonsters.monsters is the array of monsters, and myMonsters.monsters[2].getMonsterName() will be the String "Py".

Suggestion: make the monsters variable private (or at least protected), and add a method to the Monsters class to retrieve one of the monsters.
For example,

would return the MonsterTemplate at the specified index. Then you would use myMonsters.getMonster(2).getMonsterName().
You could also have a method to retrieve it by its name:

which would search for the monster with the specified name, and return that instance (or null if it is not found)

Sorry for my late reply, but I thank you for the help, and I will try this when I get the time. I am quite new to Java, and have been teaching myself, and that can be a bit difficult since I don't know what I need in order for things to work. How would I go about creating an instance of the class, also, what is the {...}?

Sorry if these questions seem somewhat obvious, I did try using google, but the way that they tend to explain it is confusing to me, and you all have been a great help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!