Reputation: 37
So I have an array which can be added to by the user through the console
When I attempt to System.out.println(the array);
, this appears:
project.Artist@31efa79f
So obviously if the artist name was John
and someone searched for John they wouldn't find it as it appears as
project.Artist@31efa79f
so how do I get the array to display and allow search for this as the user input?
How would I also go about searching the array for a particular artist using a scanner? So the scanner would allow the user to enter the required artist name and then it would search the array to see if there was a match to that name?
Upvotes: 2
Views: 101
Reputation: 32145
You can use toString() method of Arrays class
, like this:
//If you have an object array obj
Arrays.toString(obj);
// returns a String containing the obj elements
Take a look at the Java.util.Arrays.toString(Object[]) Method for further information.
Upvotes: 0
Reputation: 2614
You can write loop and iterate over all the elements and you can give response by comparing the elements of array.
and if you want to convert array in to String object then you can use
Arrays.toString(Object [] arr);
Upvotes: 0
Reputation: 48404
Use either:
Arrays.toString(myArray)
for single-dimension arrays, orArrays.deepToString(myArray)
for multiple-dimension arraysYour custom objects will have to @Override
the Object#toString
method to print a human-readable representation.
You probably want to search by property value instead, though.
So, if your MyObject
has a property artistName
, you implement a getter for that property (getArtistName
), then iterate a Collection<MyObject>
until getArtistName().equals("John")
.
Upvotes: 0