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 Delivering the MVP Forum

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

taske 4 of 4, Forum post.java

hello, im, getting close, but i need some help to finish this step...please the solution would be fine....no more hint:)

Forum.java
public class Forum {
  private String topic;

  // TODO: add a constructor that accepts a topic and sets the private field topic

  public Forum (String topic){
    this.topic = topic;
  }

  public String getTopic() {
    return topic;
  }



  public void addPost(ForumPost post) {
    System.out.printf("A new post in %s topic from %s %s about %s is available",
            topic,
            post.getAuthor().getFirstName(),
            post.getAuthor().getLastName(),
            post.getTitle()
    );
  }


}
User.java
public class User {
  // TODO: add private fields for firstName and lastName
  private String firstName;
  private String lastName;

  public User(String firstName, String lastName) {
    // TODO: set and add the private fields
    this.firstName = firstName;
    this.lastName = lastName;

  }

  // TODO: add getters for firstName and lastName
  public String getFirstName(){
    return firstName;

}


  public String getLastName(){
    return lastName;
  }
}
ForumPost.java
public class ForumPost {
  private User author;
  private String title;
  private String description;

  // TODO: add a constructor that accepts the author, title and description
  public ForumPost( User author, String title, String description){
    this.author = author;
    this.title = title;
    this.description = description;
  }

  public User getAuthor() {
    return author;
  }

  public String getTitle() {
    return title;
  }

  public String getDescription() {
    return description;
  }
}
Main.java
public class Main {

  public static void main(String[] args) {
    System.out.println("Beginning forum example");
    if (args.length < 2) {
      System.out.println("Usage: java Main <first name> <last name>");
      System.err.println("<first name> and <last name> are required");
      System.exit(1);
    }

    Forum forum = new Forum("Java");
    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User("Gonzalo","Torres del Fierro");
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost(author,"new_knowledge","welcome");
    forum.addPost(post);


  }

}

4 Answers

In Main there's a comment above the line where you create the User object. It is here:

    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User("Gonzalo","Torres del Fierro");

What this means is you want to use the args, not your own name as strings. Try:

    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User(args[0], args[1]);

The args are passed into the application when it is initially started in this line:

  public static void main(String[] args)

That holds a String array as a parameter and Craig has put his own name in the first two elements of that array. So, args[0] contains firstName and args[1] contains lastName.

Give that a go!

I hope that helps.

Steve.

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

really, ups! i did not see that wy at all.....mmm may be i´m no so smart as i thought....hehehe,

well steve, i think i´m ready for the solution post.... was too hard for me :(

Hi Gonzalo,

The error you are getting should mention the args. This refers to the arguments passed into the main method. It is the name of an array and the first two elements contain the first name and last name.

So, when creating your User instance, called author pass in args[0] for the first name and args[1] for the last name. So, use those two things in your constructor to User.

Make sense? Let me know how you get on.

Steve.

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

mmmmmm i did not get it, may be because i´m not english native....:( some times it is hard for me to get the whole idea, or just the simple message...

The solution is to replace your line:

User author = new User("Gonzalo","Torres del Fierro");

with:

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

So, amend Main.java with that.

Steve.

Can you copy and paste your code so we can get this sorted?

Steve.

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

i finally did it....was a silly misstyping, i went using "arg[0]", instead the correct "args[0]"...so, i passed, thank you very much Steve

Well done - glad you got it sorted :+1: