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

What's wrong with this code?

I am not sure what is wrong with this code.

I made two attempts to produce the desired output. They are both commented out at the bottom of the code.

They both run and they both produce, in my eyes, the desired output, depending how you interpret it and whether you are counting spaces.

It's a bit unclear whether "BlogPost: TITLE by AUTHOR" is desired to have quotes or not, so I did it both ways. I'm pretty sure italics are not desired, since that hasn't been covered in the course and no '.toItalics()' command appears to exist in the Java standard library (then again, maybe I missed it).

Anyway, I think I've successfully overridden the toString method and now I am just in a battle with a script checking program--can someone help me?

Thanks,

RCM

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.toUpperCase() + " by " + mAuthor.toUpperCase() + "\"";
    //  return "BlogPost:  " + mTitle.toUpperCase() + " by " + mAuthor.toUpperCase();
    }

}

Answering my own question here in case anyone else has this same problem.

Even though the requested output is: "BlogPost: TITLE by AUTHOR" the actually desired output does not put the title and author in uppercase. Using .toUpperCase() is the only thing that made this code "wrong."

Also, @Override should be included on the line before the method, but it is actually optional and its absence is not what caused the error.

1 Answer

Jacob Bergdahl
Jacob Bergdahl
29,118 Points

You forgot the @Override annotation, and you have two spaces after the colon :) Also, it's not supposed to be toUpperCase, I believe, TITLE and AUTHOR are simply written in uppercase to show that they're variables. Make these changes to your code, and I guarantee it'll work :)

Jacob Bergdahl
Jacob Bergdahl
29,118 Points

Ah, I just noticed that you added a comment to your own question and that you've already found the solution. Very good! I'll mark it as complete then.