• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array of Objects problem  RSS feed

 
John Downs
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. I have an Array of objects that contains strings. How can I get the object's strings to print in a list so that the user can select that object to manipulate its attributes? For example, the user can select "Guitar 1" from a list and manipulate its attributes like tuning it, playing it, etc. I have a class called Instruments and created 10 guitar objects. Here is the code:

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem is a simple design issue which can be solved by following this guideline: StopCoding
 
John Downs
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I can appreciate that, it doesn't help me with my problem.
 
John Downs
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added more description to my problem. This is what I am having an issue with. I am trying to figure out how to print the elements of my array into a list. When I print the array, all I get is the memory locations.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you print an object, System.out.println() uses a method called toString(). You need to override this method or you will get Object's toString() method which does what you're describing.

Also, you have a bunch of code in your for loop that doesn't need to be there.
 
John Downs
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help! Using toString worked. Also, what do you mean I have a bunch of code that doesn't need to be there?
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downs wrote:Thanks for the help! Using toString worked.

You're welcome. Glad it worked.
Also, what do you mean I have a bunch of code that doesn't need to be there?[


Well, you have this (and a bunch of lines like it) in the for loop:

How many times does it execute? How many times does it *need* to execute?

(We like to give hints at the Ranch.)
 
John Downs
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I am trying to achieve:

There is a loop that will process through the ten instances.
The loop begins with a list of the ten instances followed by a prompt requesting one of the instruments on the list. NOTES: It is not acceptable to use 0 as a reference to any item on the list. There is no exit selection in this list.
When one of the ten instruments is selected there is a call to the appropriate method in the Class with two arguments: (1) the index number of the array and (2) the name of the specific instrument.
Once the processing of one instance is completed, the user is asked to modify another instance or to end the program.
If the user response is to modify, then the list of instruments is displayed again. A new instrument is selected and the process begins again with all the appropriate processing. It is expected that all the attributes initialized again.

If I delete the individual indexes, the loop prints but the values are all null. That's not what I'm trying to achieve.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it would help if I had more code, but from your snippet it looks like this would be better:



guitar[0] thru 9 don't change inside the loop, so put them outside
 
John Downs
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for getting me thinking about the loop. I see what you mean now. This is what i came up with:



Is that better?
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. The nice thing about posting code here is you can get mini code reviews on it and improve your skill.
 
John Downs
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have another question that I hope someone could help me with. First off, this is what i have so far:



How do I go about assigning a variable to the index of my array that the user just selected? Thanks again for all the help thus far.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on how the user is selecting a guitar. I don't see any code where there is user selection.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I told you to follow this guideline was for you to develop skills that's required for programming...

Seeing that you are a follow by example kind of lad... let's get down to business then...

What I assume you are trying to accomplish is create and provide a list of instruments for the user to choose from... Upon user's choice, retrieve the instrument from the list and give it to the user to perform operations on the instrument such as tuning, playing, etc...

Seeing that your requirement is to use an array to create the list we declare it as follows:


Next we use a for loop to initialize the list of ten guitars:


Next we present the list to the user to choose from:


Next prompt the user for choice:


Next retrieve the guitar from the list:


Now you can perform operations on the user's retrieved guitar:


Putting it all together in main we conclude:


Note: This is just illustrative and can be done in many improved ways...
 
John Downs
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the illustration! That was very helpful! I am a "noob", beginner, etc. when it comes to Java but I want to learn all I can. I'm glad I joined this board!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!