Forums Register Login

Getting specific values from an array.

+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 397 times.
Similar Threads
java beginner, need help with code
Long list of if statements: is there a better way to do this?
Inheritance with abstract classes
Interface and using Polymorphism (I think?) -- Game I'm making and need help.
Conceptual Misunderstanding of Arraylists?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:59:07.