javaeclipse-luna

How can I print a new line in a toString() method in java


Instead of the many system.out.println() lines, I want to write System.out.println(b) in TestBook.java file. So what should I write in the toString() in the Book class to return the same results to look like below

//Title: xxx
//Author:xxx
//Years:xxx
//Pages:xxx
//edition: xxxx
//==================================

public class Book {

String title;
String author;
int yearOfPublishing;
int numberOfPages;
int eddition;

Book ()
{}

Book ( String title, String author, int yop, int nop, int eddition)
{
    this.title = title;
    this.author = author;
    yearOfPublishing = yop;
    numberOfPages = nop;
    this.eddition = eddition;

}

public String toString()
    {
    // return what?? how can i return new lines
    }
}

public class TestBook {

    public static void main(String[] args) {

        Book b = new Book("Data", "Joe", 2015, 276, 3);

        System.out.println ( "Title : " +b.title);
        System.out.println ( "Author : " +b.author);
        System.out.println ( "Year : " +b.yearOfPublishing);
        System.out.println ( "Pages : " +b.numberOfPages);
        System.out.println ( "Eddition : " +b.eddition);    
        System.out.println ("==================================");

    }
}

Solution