Reputation: 241
My program should return the different attributes for two different objects. In my main method I set those attributes as the arguments while creating new objects. But when I call those getter methods (which I have written in a separate class, I can post that class if needed), it doesn't return all the attributes. It only prints out the first attributes (which is also set as first argument), not the other two values. I don't know where I have done wrong.
my code: Main class:
package main;
public class Main {
public static void main(String[] args) {
//creating object for book 1
Book book1 = new Book("The brief history of time", "111", new String[]{"S. hawking", "hawking's friends"});
//creating object for book 2
Book book2 = new Book("100 years of solitude", "222", new String[]{"G.marquez", "marquez's friend"});
System.out.println("All info for the first book: \n");
System.out.println("Name: " + book1.getName());
System.out.println("ISBN: " + book1.getIsbn());
System.out.println("Authors: " + book1.getAuthors());
System.out.println("\n\n");
System.out.println("All info for the second book: \n");
System.out.println("Name: " + book2.getName());
System.out.println("ISBN: " + book2.getIsbn());
System.out.println("Authors: " + book2.getAuthors());
}
}
Book class:
package main;
public class Book {
//variables
private String name;
private String isbn;
private String[] authors;
//constructors
public Book(String name, String isbn, String[] authors) {
this.name = name;
this.isbn = name;
this.authors = authors;
}
//setters
public void setName(String name) {
this.name = name;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public void setAuthors(String[] authors) {
this.authors = authors;
}
//getters
public String getName() {
return name;
}
public String getIsbn() {
return isbn;
}
public String[] getAuthors() {
return authors;
}
}
Upvotes: 1
Views: 1403
Reputation: 1048
There is a problem with Book constructor:
public Book(String name, String isbn, String[] authors) {
this.name = name;
this.isbn = name; // you are setting isbn as name!
this.authors = authors;
}
I think you need to define how getAuthors prints the authors array, like What's the simplest way to print a Java array?
Upvotes: 0
Reputation: 848
When you want to print a string array you can use this .
System.out.println(Arrays.toString(book1.getAuthors()));
Upvotes: 0
Reputation: 941
You need to iterate the authors
array in order to print the Strings inside it. something like this:
System.out.println("All info for the first book: \n");
System.out.println("Name: " + book1.getName());
System.out.println("ISBN: " + book1.getIsbn());
for (String author : book1.getAuthors()) {
System.out.println("Author: " + author);
}
Also there's a problem in your Book
class constructor:
public Book(String name, String isbn, String[] authors) {
this.name = name;
this.isbn = name; // this.isbn is not name!
this.authors = authors;
}
must be:
public Book(String name, String isbn, String[] authors) {
this.name = name;
this.isbn = isbn;
this.authors = authors;
}
Upvotes: 1