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 Objects (Retired) Delivering the MVP Wrapping up

Preston Yeschick
Preston Yeschick
10,803 Points

Output is appropriate in editor but still fails challenge

My code outputs what appears to be an appropriate string, including the author name, post title and post description. The challenge returns, "I expected to see the first and last name input as args in the output, but I do not".

ForumPost.java
public class ForumPost {
  private User mAuthor;
  private String mTitle;
  private String mDescription;

  public ForumPost(User author, String title, String description) {
    mAuthor = author;
    mTitle = title;
    mDescription = description;
  }

  public String getDescription() {
   return mDescription; 
  }

  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  // TODO: We need to expose the description
}
User.java
public class User {
  private String mFirstName;
  private String mLastName;

  public User(String firstName, String lastName) {
    // TODO:  Set the private fields here
    mFirstName = firstName;
    mLastName = lastName;
  }

  public String getFirstName() {
    return mFirstName;
  }

  public String getLastName() {
   return mLastName; 
  }

}
Forum.java
public class Forum {
  private String mTopic;

  public Forum(String topic) {
    mTopic = topic;
  }

  public String getTopic() {
      return mTopic;
  }


  public void addPost(ForumPost post) {
      /* When all is ready uncomment this...*/
      System.out.printf("New post from %s %s about %s.\n",
                         post.getAuthor().getFirstName(),
                         post.getAuthor().getLastName(),
                         post.getTitle());

  }
}
Example.java
public class Example {

  public static void main(String[] args) {
    System.out.println("Starting forum example...");
    if (args.length < 2) {
       System.out.println("first and last name are required. eg:  java Example Craig Dennis");
    }
     Forum forum = new Forum("Java");
    // Take the first two elements passed args
     User author = new User("Preston", "Yeschick");
    // Add the author, title and description
     ForumPost post = new ForumPost(author, "my title", "my description");
     forum.addPost(post);
  }
}

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hey Preston,

This was a tricky challenge as it was quite vague on the specifics. You've technically got everything right but the code checker wants something a bit different. Let's look at this:

CodeSnippet.java
User author = new User("Preston", "Yeschick");

Now logically speaking, we can see that the User class takes in a first name and last name string as the constructor parameters right? And your completely right. But now let's look at this top part in our Example.java file:

Example.java
public static void main(String[] args) {
    System.out.println("Starting forum example...");
    if (args.length < 2) {
       System.out.println("first and last name are required. eg:  java Example Craig Dennis");
    }

We haven't really worked with args so when I did this challenge way back when, I just glanced over this part not really paying attention to it and so I was confused as well. It looks like some weird foreign piece of code !

But anyways, what this is saying is that when we start our program up, we pass in an array of strings as a parameter value. Something like this:

ArrayExample.java
["Prestion", "Yeschick"];

This will be an array that has a first and last name which the code checker will input automatically. If we have two values in our array, then we can use that as our first and last name right? In that if statement, we make sure that we have at least 2 things in our array otherwise we wouldn't be able to have a first and last name. If we passed in more than 2 things, that wouldn't matter as we are only dealing with the first two things in the array. Did that make sense?

So now back to the code, let's look at this:

AnswerSnippet.java
User author = new User(args[0], args[1]);

We will take the first thing out of that array (the first name) and the second thing out of the array (the last name); That's basically what it wanted you to do and not to input your own strings directly.

I hope that helped you out and you now understand the challenge!!

Happy coding and best of luck :smile: ,

Kevin

Preston Yeschick
Preston Yeschick
10,803 Points

I see now. Thanks for taking the time to write a robust explanation. I'm on track to finishing now. Thank you