-1

Been following this (https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html#zz-2.2) for study java OOP. There is three java file. Now I tested public Author getauthors() {return authors[0]} will return the first author full into: name, email and gender. But I want iterate the authors array to get all the author name only. what should I write in the Books java module?

  1. One is pretty simple, used to describe author class. It have three attributes: private String name;private String email;private String gender; the file already tested. All is fine.

  2. The Book file to describe the book. One book can have multiple authors.

    public class Book { private String name;private int qty; private double price; Author authors[];

     public Book(String name, int qty, double price) {
         this.name = name; this.qty = qty; this.price = price;}
     public Book(String name, Author authors[], int qty, double price) {
         this.name = name; this.authors = authors;this.qty = qty; this.price = price;}
    
     public String getName() {return name;}
     public void setName(String name) {this.name = name;}
     public double getPrice() {return price;}
     public void setPrice(double price) {this.price = price;}
     public int getQty() {return qty;}
     public void setQty(int qty) {this.qty =qty;}
     public void setauthor(Author[] author) {this.authors = authors;}}
    
  3. The third file to test all the methods coded in the first two file.

     public class testauthor {
             public static void main(String[] args) {
             Author[] authorstest = new Author[2];
             authorstest[0] = new Author("Tan Ah Teck", "ahteck@nowhere.com", "m");
             authorstest[1] = new Author("Tyler Cowen", "tcowen@gmu.edu", "M");           
       Book dummyBook = new Book("small steps toward a much better world",authorstest, 19, 99);System.out.println(dummyBook.getName());}}
    
jian
  • 4,119
  • 1
  • 17
  • 32
  • To me, it's not clear what exactly you are struggling with. Could you provide a little more detail? Do you want to print the names of all `Author` elements within an array? – MC Emperor May 17 '21 at 08:03
  • Try my best to make the code readable.... – jian May 17 '21 at 08:04
  • @MCEmperor one book two author. 'public Author getauthors() {return authors[0]}' can get me the first author info. But I want extract/get the book two author's name. Author info is put in an array. – jian May 17 '21 at 08:06
  • Use a `for` loop: `for (int i = 0; i < authors.length; i++) { author[i].getName(); }`, or shorter: `for (Author author : authors) { author.getName(); }`. – MC Emperor May 17 '21 at 08:07
  • @MCEmperor `public Author getauthors() {return authors[0]}` this will work. the result is `name : Tan Ah Teck, gender : m, email : ahteck@nowhere.com `Since also the method need to return something. So I Still stuck..... – jian May 17 '21 at 08:32
  • `public String[] getauthors() {String[] stringArray1; for (int i = 0; i < authors.length; i++) {stringArray1= authors[i].getName(); } return stringArray1;}` The warning is `C:\Users\JIAN HE\Desktop\myjava1>javac testauthor.java .\Book.java:24: error: incompatible types: String cannot be converted to String[] stringArray1= authors[i].getName(); }` So far it's very close. – jian May 17 '21 at 09:02

2 Answers2

1

Well, if you want to return the names of all authors, you have to make a choice here:

  • Either you return a String[], where each element is the name of one of the authors;

  • Or you return a String, where the authors are listed, say, with commas in between. For example: J. He, J. Skeet, M. C. Emperor.

    You could use a for loop for this:

    String authorsText = "";
    for (int i = 0; i < authors.length; i++) {
        if (i > 0) {
            authorsText += ", ";
        }
        authorsText += authors[i].getName();
    }
    

Further:

  • It is a little peculiar that you can set a book's author after construction. Is the author of a book ever changed after the book is published? The same goes for a book's title. I would have removed those setters, as they don't make sense to me.
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • I thanks. So far I reached here. `public String[] getauthors() {String[] stringarray = new String[authors.length]; System.out.println(authors.length); for (int i = 0; i < authors.length; i++) {stringarray[i]=authors[i].getName();} return stringarray;} ` but the result is `3 [Ljava.lang.String;@4617c264` I do `import java.util.*;` – jian May 17 '21 at 09:40
  • return a `String[]` Can not print out. So I need to have a nested methods to print out all the elements. That would be too hard for me. Can you tell me how to use String[] ways to resolve the issue.... – jian May 17 '21 at 11:49
  • Well, [here's how to print out arrays](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array). But you also seem to be missing some of the basics, like using a `for` loop, or the meaning of return types. I suggest you [study the basics](https://docs.oracle.com/javase/tutorial/java/index.html). I think that this tutorial provides a good understanding of Java. – MC Emperor May 17 '21 at 11:57
  • I thank you. I finished W3C JAVA Tutorial. But I encountered another problem. I hope you can help me..... https://stackoverflow.com/questions/67580517/how-to-hold-the-value-from-the-userinput-in-java – jian May 18 '21 at 06:11
0

you can use stream to get all Names :

Arrays.stream(authors).map(a->a.getName()).collect(Collectors.joining(","));

this get all names and separated by coma

Omar Hussien
  • 313
  • 1
  • 9