0

I'm a begginer doing some exercises on Java OOP so here's my problem. I have a Book class with this attribute:

private Author[] authors;

I need a method that returns just the names of those authors(name1,name2,..). The Authors class has a getName() method:

public String getName() {
        return name;
}

And I tried following code but it doesn't work !

//Method in the Book class
public String getAuthorsNames(){
    return authors.getName();
}

Do I need to loop through the array or is there another way ?

schlebe
  • 3,387
  • 5
  • 37
  • 50
jgphoenix
  • 3
  • 2
  • 1
    Do you want to return a `String[]` or a single `String`? And if you really want a single `String` as your code suggests, what should it look like? – Thomas Apr 08 '22 at 20:54
  • If you've got multiple authors, then there are multiple names. What single string do you expect to return? – khelwood Apr 08 '22 at 20:54
  • @Thomas just a single `String` of the names separated by a comma eg. "name1,name2,name3" – jgphoenix Apr 08 '22 at 20:57
  • authors is not a class. It is an array of Author. Yes, you need to loop through the array or use a stream. – OldProgrammer Apr 08 '22 at 20:57
  • @OldProgrammer i'm not familiar with what a stream is yet, can you give me an example? – jgphoenix Apr 08 '22 at 21:02
  • 1
    Like this: [Join a list of object's properties into a String](https://stackoverflow.com/questions/44245790/join-a-list-of-objects-properties-into-a-string) but [creating the stream from the array.](https://stackoverflow.com/questions/27888429/how-can-i-create-a-stream-from-an-array) – khelwood Apr 08 '22 at 21:04
  • While it's not exactly a beginner concept, a stream in this case would be `Stream.of(this.authors).map(Author::getName).collect(Collectors.joining(","))` – Rogue Apr 10 '22 at 19:26

1 Answers1

0

private Author[] authors; is array of object Author

you need to add the index then get the name, here is an example:

class Author {

  private String name;

  public Author(String name) {
    this.name = name;
  }
  
  public String getName() {
    return this.name;
  }

and in your class Book:

  class Book {
    private Author[] authors;
    public Book(int authorsSize) {
      authors = new Author[authorsSize];
    }
    
    public void setAuthor(int index) {
      this.authors[index] = new Author("Author Name"):
    }

   public String getAuthorName(int index) {
      return this.authors[index].getName();
   }

   public String getAllAuthors() {
     String all = "";
     for (int i = 0; i < authors.length; i++) {
         all += authors[i].getName() + ", ";
     }
     
     return all;
   }

  }

After adding Authors .. use getAllAuthors

--- more --- Instead of Author[] authors = new Authors[size]; You can use ArrayList<Author> authors = new ArrayList<>(); then you can use:

authors.add(new Author("Author name1"));
authors.add(new Author("Author name2"));
authors.add(new Author("Author name3"));
authors.add(new Author("Author name4"));
......
obeid salem
  • 129
  • 2
  • 13