Home > Back-end >  Why doesn't the objects in my array display correctly?
Why doesn't the objects in my array display correctly?

Time:12-12

How do I print the first and last name of the author of every object in the array? I've tried adding a .first_name, but it doesn't work.

This is the code in the main class:

public class Main {
    public static void main(String[] args) {

        Book book1 = new Book("The Alchemist", "Paulo Coelho", "HarperCollins", "Adventure");
        Book book2 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "Charles Scribner\'s Sons", "Fiction");
        Book book3 = new Book("The Catcher in the Rye", "J. D. Salinger", "Little, Brown and Company", "Fiction");

// Create an instance of the class Author

        Author author1 = new Author("Paulo", "Coelho");
        Author author2 = new Author("F. Scott", "Fitzgerald");
        Author author3 = new Author("J. D.", "Salinger");

// Create an instance of the class Publisher

        Publisher publisher1 = new Publisher("HarperCollins");
        Publisher publisher2 = new Publisher("Charles Scribner\'s Sons");
        Publisher publisher3 = new Publisher("Little, Brown and Company");

        String[] authors = {author1.toString(), author2.toString(), author3.toString()};
        String[] publishers = {publisher1.toString(), publisher2.toString(), publisher3.toString()};
        String[] books = {book1.toString(), book2.toString(), book3.toString()};

        displayBooks(books);
        displayAuthors(authors);
        displayPublishers(publishers);

    }
    public static void displayAuthors(String[] authors){
        for(String author : authors){
            System.out.println(author);
        }
    }
    public static void displayPublishers(String[] publishers){
        for(String publisher : publishers){
            System.out.println(publisher);
        }
    }
    public static void displayBooks(String[] books) {
        for (String book : books) {
            System.out.println(book);
        }
    }
}

And for the constructor of author:

public class Author {

    String first_name;
    String last_name;

    Author(String first_name, String last_name){
        this.first_name = first_name;
        this.last_name = last_name;
    }
}

CodePudding user response:

When you print a java object, it internally looks for the toString method for that class and prints the value returned by that method. In case of a custom class, as the one you created, there is no definition of toString so it defaults to the one implicitly inherited from the Object class.

To quote the Java documentation Link

Class Object is the root of the class hierarchy. Every class has Object as a superclass.

If you want to customize the behavior, you need to Override the toString method in your class.

public class Author {

    String firstName;
    String lastName;

    public Author(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    @Override
    public String toString() { // <-- Overridden toString method
        return this.firstName   " "   this.lastName;
    }
}

NOTE: I have changed the fields and parameter names to the recommended camelCase notation for Java

You have to do the same for all the classes you want to have a custom String representation printed.

CodePudding user response:

You can try this:

 public class Main {
    public static void main(String[] args) {

        // Create an instance of the class Author

        Author author1 = new Author("Paulo", "Coelho");
        Author author2 = new Author("F. Scott", "Fitzgerald");
        Author author3 = new Author("J. D.", "Salinger");

        String[] authors = {author1.toString(), author2.toString(), author3.toString()};
      
         displayAuthors(authors);

    }

    public static void displayAuthors(String[] authors){
        for(String author : authors){
            System.out.println(author);
        }
    }
    

    public class Author {

        String first_name;
        String last_name;
        
        Author(String first_name, String last_name){
            this.first_name = first_name;
            this.last_name = last_name;
        
        }
        @Override
        public String toString() {
            return this.first_name " " this.last_name;
        }
    }
}
Paulo Coelho
F. Scott Fitzgerald
J. D. Salinger

Note : as .toString() method prints the memory address of the Object, you have to override the method according to your need.

  • Related