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

Alex Rodriguez
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Rodriguez
Front End Web Development Techdegree Student 20,810 Points

Error: I expected to see the names I passed in through the args, but I do not. Hmm. Preview

I am not sure what I am missing here. I have:

User author = new User("King", "Zero") ForumPost post = new ForumPost(author, "amazing", "yes!!")

I thought that when i add author to the ForumPost it would take the strings I stored in author and add them to ForumPost.

What am I doing wrong?

Thank you

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 User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getDescription() {
    return mDescription;
  }


}
User.java
public class User {
  private String mFirstName;
  private String mLastName;

  public User(String firstName, String lastName) {
   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) {
      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("King", "Zero");
    // Add the author, title and description
    ForumPost post = new ForumPost(author, "amazing", "yes!!");
    forum.addPost(post);
  }
}

2 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Alex,

In this example Craig Dennis is wanting you to pass in the two values passed into our args array that is is in the main method.

We can do this by

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

Remember that arrays start at index 0.

Essentially what this does is when we compile our program we pass the name in then

java Example.com("Rob Bridges");

Thanks, let me know if this doesn't help, and I'll try to clarify.

Alex Rodriguez
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Rodriguez
Front End Web Development Techdegree Student 20,810 Points

Thank you Rob! It did help me pass the challenge but I am still confused on User author = new User(args[0], args[1]);

I don't understand what args[0], args [1] are doing? Where is the name coming from?

Why can't it be User author = new User("Alex", "Rodriguez");

If you can please help understand this or point me in the right direction, that would be awesome.

Thank you

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Alex the args array comes from our main method.

public static void main(String args[]) 

and its there to catch any input we may pass in while loading our program. Say you want to parse a file when using the command line or terminal wed type. java Example(fileToBeParsed)

Or in this instance when loading our program Craig set the grading check to pull from Args, I think to try to make us think and work through a problem we wouldn't expect. Craig's like that.

Essentially we use args to pass any information into our program when we initially load and compile it.

Thanks I hope this helps.