Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Getting There Override an inherited method

Override the toString method from java.lang.Object and make it return the following information: "BlogPost: TITLE by A

ive been stuck for to long plz help

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost {
    private String mAuthor;
    private String mTitle;
    private String mBody;
    private String mCategory;
    private Date mCreationDate;

    public BlogPost(String author, String title, String body, String category, Date creationDate) {
      mAuthor = author;
      mTitle = title;
      mBody = body;
      mCategory = category;
      mCreationDate = creationDate;
    }

    public String getAuthor() {
      return mAuthor;
    }

    public String getTitle() {
      return mTitle;
    }

    public String getBody() {
      return mBody;
    }

    public String getCategory() {
      return mCategory;
    }

    public Date getCreationDate() {
      return mCreationDate;
    }

    public String toString(){ 
      return "BlogPost: \""+mTitle + "\" -@" mAuthor;
    }
}
Simon Coates
Simon Coates
28,694 Points

you're missing a + sign prior to mAuthor in the toString method. It also requires use of the override tag.

2 Answers

Simon Coates
Simon Coates
28,694 Points

try

    @Override
    public String toString(){ 
      return "BlogPost: "+mTitle + " by "+ mAuthor;
    }
Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Harrison,

This is a great challenge so I don't want to just give the answer, however I'll give you some help. There is a very useful way to Format Strings without concatenation or calling an IO device.

It's showed below.

String.format("");

We can use this method just like it was a printf method, meaning that pass in place holders and plug in values.

So really, our answer would begin with

@Override 
public String toString() {
Return String.format(" // hmmmm I need to pass in the string and add in values like the title and mAuthor method, I wonder if we have methods that return those in this class? "))

Thanks, I hope this helps.