user4707997
user4707997

Reputation: 37

How can I convert an object array to string for searching purposes?

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

Answers (3)

cнŝdk
cнŝdk

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

Prashant
Prashant

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

Mena
Mena

Reputation: 48404

Use either:

  • Arrays.toString(myArray) for single-dimension arrays, or
  • Arrays.deepToString(myArray) for multiple-dimension arrays

Your 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

Related Questions