0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
C.Kontosis
  • 11
  • 3
  • 2
    You have to write your own print loop if you don't want the `[ , ]` characters in the output. Or use `Stream` with `joining()`. – Andreas Mar 13 '21 at 07:44
  • I wonder that `books.toString()` is even giving that output (`books` being an array) –  Mar 13 '21 at 08:22
  • @user15358848 he's added a new line in `Book.toString()` each book entry toString is yielding new line. :p – the Hutt Mar 13 '21 at 09:01
  • 3
    @onkar that is not the point. Arrays do not implement the `toString()` method (OP posted `books.toString()`), so the version of `Object` is used, which does not output the content of the array (e.g. see https://stackoverflow.com/q/409784/15358848) –  Mar 13 '21 at 09:03

4 Answers4

0

From your code I observe that you override toString() method on object class so in this case you should use a for loop for print every object or you can create a new method. The new method must be has a loop for print all object

public string printAll(Book[] books){
//then for/while loop to iterate every element//
//then print
}

I think this will work

0

If you want to print the books, which is an array of Book, the way you want it, you can do this.

System.out.println(Arrays.stream(books).map(Object::toString).collect(Collectors.joining()));
0

The type of books seems to be List or another collection, not an array of books Book[], if it prints the mentioned output.

Thus, if books is an iterable collection, its method forEach may be used instead of loop:

books.forEach(System.out::println);

Or, if books need to be converted into a single String, Collectors.joining should be used as Andreas mentioned in the comment:

String toPrint = books.stream().map(Book::toString).collect(Collectors.joining("\n"));
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
0

Create a static method in Book class:

public static void prettyPrintBooks(Book[] books) {
  //print header

  for(Book b : books){
     //print each book row
  }

}

You can call this method like: Book.prettyPrintBooks(books);

the Hutt
  • 16,980
  • 2
  • 14
  • 44