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

Help with Last Part. Can't get last code to work.

When I input the variables getFirstName, getLastName, author, title, and description. It doesn't work and I get errors. What am I doing wrong?

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(getFirstName, getLastName);
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost(author, title, description);
    forum.addPost(post);
  }

}

1 Answer

This one is a bit tricky. lets focus on Main.java.

There are two TODOs to address. The first todo asks that you supply the parameters necessary to instantiate a User. If you check the User class, you know this means you need a String of firstName, and String of lastName. So how can we reference the first name and last name the user types into the console?

in the video, Craig points out that you can access command line arguments that are typed into the console because they are stored in the args (or arguments) array. items stored in an array have a number assigned to them (aka an index) so they can be easily retrieved. The first item in the array has an index of 0, the second item has an index of 1, etc.

if you look at the code in main.java, you can see that the system.out.println indicates that the user will be prompted to enter the firstname first, and the lastname second. so, to access those array items, we need to use the array variable args[], but in the brackets we need to insert the index number we'd like to reference. in this case, firstname should be the first argument, or args[0], and last name should be the next argument in the array, or args[1]. (If none of this really makes sense, give the video another watch and look for the argument array explanation.)

The second bit of the challenge is just making sure we put the correct parameters in the new ForumPost. in this case, the author variable we created in the first half, and then, per the TODO comments, any kind of title String and anykind of description String.

See the code below:

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>"); //<-- notice here how the user will be prompted to input the author names in a certain order?
      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(args[0], args[1]); //<-- here are the two index values for the firstname and last name as they are stored in the args array
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost(author, "Hey, a Title String!", "Hey, a description string!"); //<-- here is our author, title, and description.
    forum.addPost(post);

  }

}