Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Using \n escape sequence in a toString method

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Using \n escape sequence in a toString method

    Hello all,
    I'm working on a homework problem that's due this evening. The prob asks to create a toString method that returns a "nicely formatted, multi-line description" of the class objects I created. The class is called "Book" and its objects are title, author, publisher, and copyright date. My toString code trying to use \n for a new line is as follows:

    return ("Book - Title: "+ title\n"Author: "+ author\n"Publisher: " + publisher\n"Copyright: " + copyright);

    This gives me several errors: syntax errors on the token n, syntax error insert ")" to complete expression, n cannot be resolved to a variable, syntax error on token "Invalid Character", ( expected...where am I going wrong? The only example I found in my text book uses the \n sequence in a System.out.println method. Can this even be used in a toString method?

    I'm veeeeery new to java/code writing so any help would be much appreciated.

    Thanks,
    -C


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Using \n escape sequence in a toString method

    \n needs to be within the quotations and you need to concatenate each variable/String with the + operator.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using \n escape sequence in a toString method

    Thanks for the help newbie! This did help create newlines for each item, but now its calling Title: " + title", Author: "+ author" instead of getting the data I set up for the 3 books I created in my driver class. Here's the code for my Book class and the code for driver class called Bookshelf...do you see any problems with them?
    public class Book {
    // Instance data
    private String title;
    private String author;
    private String publisher;
    private int copyright;



    // Constructor
    public Book(String title, String author, String publisher, int copyright) {
    this.title = title;
    this.author = author;
    this.publisher = publisher;
    this.copyright = copyright;
    }


    // getters
    public String getTitle() {
    return title;
    }

    public String getAuthor() {
    return author;
    }

    public String getPublisher() {
    return publisher;
    }

    public int getCopyright() {
    return copyright;
    }

    //setters
    public void setTitle(String newTitle) {
    title = newTitle;
    }

    public void setAuthor(String newAuthor) {
    author = newAuthor;
    }

    public void setPublisher(String newPublisher) {
    publisher = newPublisher;
    }

    public void setCopyright(int newCopyright) {
    copyright = newCopyright;
    }

    public String toString () {
    return ("Book - Title: + title\n" +"Author: + author\n" + "Publisher: + publisher\n" + "Copyright: " + copyright);

    public class Bookshelf {

    public static void main(String[] args) {

    Book b1, b2, b3;

    b1 = new Book("Schizos Are Never Alone, ","Sigmund Freud, ","Brutal Books, ", 1901);
    b2 = new Book("Hitchhiker's guide to Mexico, ","Felipe Caldron, ","Leer Libros, ", 2009);
    b3 = new Book("Forget Keshawn, Throw ME the Damn Ball, ","Chad Johnson, ","Happy Times Press, ", 2011);

    System.out.println("b1: " + b1); //implicitly calls d1.toString()
    System.out.println("b2: " + b2.toString()); //explicitly calls d2.toString()
    System.out.println("b3: " + b3);


    Thanks,
    -C

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Using \n escape sequence in a toString method

    + title needs to be outside of the String quotations, only \n needs to be inside.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. The Following User Says Thank You to newbie For This Useful Post:

    coolidge (September 22nd, 2011)

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using \n escape sequence in a toString method

    Thanks again newbie....I got it working great now.

    -C

Similar Threads

  1. Input/Output Sequence
    By JoseGuad in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 30th, 2011, 06:50 AM
  2. Data sequence
    By street_missile in forum Java ME (Mobile Edition)
    Replies: 10
    Last Post: August 26th, 2011, 11:54 AM
  3. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 06:00 AM
  4. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 05:44 AM