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

Attila Tamás
Attila Tamás
1,168 Points

Can someone help with the 4th Forum challenge?

I'm a little lost at the 4th challenge, that goes like this: "After you uncomment the code in Main.java and Forum.java, fix the code as described in the comments of Main.java. The code in main is attempting to use the code you just fixed."

Can someone help please? :)

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.Forum.java();
  }

  public String getTopic() {
    return topic;
  }

  /* Uncomment this when you are prompted to do so*/
  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
    firstName = "";
    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){
    author = "";
    title = "";
    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);
    }
    //Uncomment this when prompted
    Forum forum = new Forum("Java");
    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User(String getFirstName(), String getLastName());
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost(user.getFirstName().user.getLastName());
    forum.addPost(post);
// 
  }

}

2 Answers

The first todo says ‘pass the first name and last name that are in the args parameter’. The names are passed into the main method call. The args parameter is a String array and the first index of the args array is the first name, the second indexs of the args areay is the last name.

Attila Tamás
Attila Tamás
1,168 Points

How would it look like in code,

Attila Tamás
Attila Tamás
1,168 Points

Please can you just give the solution? I can't solve it :D

Attila Tamás
Attila Tamás
1,168 Points

What? But i tried it like that, and gave me compile error.. Thank you so much, i must have missed something then. :)

Attila Tamás
Attila Tamás
1,168 Points

Okay, now i got it. I corrected a constructor, by initialising them like

this.variable = variable;

instead of

variable = "";

I got a messange saying "Are you sure i passed down the right variables, like args[0] should be the firstName and args[1] should be the lastName." I corrected this error, by correcting every single constructor the way i told above. Now i completed the challenge, and i'm happy :D

Here is a reference to understanding the args parameter passed into the main method.

https://www.quora.com/What-does-the-String-args-in-a-Java-main-method-actually-mean

User author = new User(arg[0], arg[1]);
Attila Tamás
Attila Tamás
1,168 Points
Forum forum = new Forum("Java");
    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User(args[0], args[1]);
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost(getAuthor(), "Title", "description");
    forum.addPost(post);

I am here currently, What did i do wrong? Still don't know what the mistake is.