I have trouble aligning the output of an array[] of Objects when calling the .toString
method.
System.out.println(books.toString());
and formatted my .toString()
to get the alignment of the object variables like this
public static String header(){
return "%1$-25s %2$-20s %3$-20s %4$-1s";
}
@Override
public String toString(){
return String.format(header(), title, author, copies, "\n");
}
Bellow is the result I get and I would like to remove the first character of each row and have the book names ,in this case, lined up
[To Kill a Mockingbird Harper Lee 30
, 1984 George Orwell 3
, The Lord of the Rings J.R.R. Tolkien 78
, The Book Thief Markus Zusak 32
, The Grapes of Wrath John Steinbeck 67
]
like this
To Kill a Mockingbird Harper Lee 30
1984 George Orwell 3
The Lord of the Rings J.R.R. Tolkien 78
The Book Thief Markus Zusak 32
The Grapes of Wrath John Steinbeck 67
Is it possible to do this with the .toString() or would I necesarily have to go with a loop for every Object to output my data?