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

Gaspar Santiago
Gaspar Santiago
3,377 Points

Bummer! I expected to see the names I passed in through the args, but I do not. Hmm. < I can't get pass this!

Here is the Example class I have:

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");
       System.out.println("java Example Craig Dennis");
    }

    Forum forum = new Forum("Java");
    User author = new User("Craig", "Dennis");
    ForumPost post = new ForumPost(author, "Example", "is awesome");
    forum.addPost(post);
}

}

Can anyone tell me what the error even means? I've tried two recommendations and so far none of them have worked. I just can't see what I'm doing wrong. All I keep getting is that "Bummer!" outcome.

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

  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  // TODO: We need to expose the description
}
User.java
public class User {

  public User(String firstName, String lastName) {
    // TODO:  Set the private fields here
  }
}
Forum.java
public class Forum {
  private String mTopic;

  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();
    // Add the author, title and description
    // ForumPost post = new ForumPost();
    // forum.addPost(post);
  }
}

2 Answers

andren
andren
28,558 Points

The comments in the Example.java file states this: "Take the first two elements passed args"

While I'll admit that is phrased somewhat poorly, what that is trying to tell you is that two arguments will be passed to the application and you have to use them, when an argument is supplied to a command line application it is access through the args array.

The fact that this comment is placed before the comment telling you to create a new user, combined with this check at the top of the file:

          if (args.length < 2) 
    {
       System.out.println("first and last name are required. eg:  java Example Craig Dennis");
       System.out.println("java Example Craig Dennis");
    }

Suggests that the arguments passed to the application are meant to be used as the first and last name of a user.

In the code that you posted you don't use the arguments to set the name of the new user, you hardcode the names instead, which is not what the task is asking you to do.

If you change "User author = new User("Craig", "Dennis");" to "User author = new User(args[0], args[1]);" that should be enough to pass the challenge.

Gaspar Santiago
Gaspar Santiago
3,377 Points

I tried that but that throws an ArrayIndexOutOfBoundsException.

andren
andren
28,558 Points

Are you sure you are using args[0] and args[1], not something like args[1] and args[2]?

You should not be getting ArrayIndexOutOfBoundsException when using the former.

I completed the task myself using the code I provided so I have no idea what your issue is, the only thing I can do is post the solution code I wrote for the task, that I know will pass it since I've tested it twice now, hopefully you can compare your own code to the solution and see if you find an error in your code.

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

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;
  }
}

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;
  }
}

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");
    User author = new User(args[0], args[1]);
    ForumPost post = new ForumPost(author, "Example", "is awesome");
    forum.addPost(post);
  }
}

If you paste all of that code into the corresponding java files at the start of the challenge then all of the tasks will complete.

Gaspar Santiago
Gaspar Santiago
3,377 Points

Here's what I have that's throwing me the exception:

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");
       System.out.println("java Example Craig Dennis");
    }

    Forum forum = new Forum("Java");
    User author = new User(args[0], args[1]);
    ForumPost post = new ForumPost(author, "Example", "is awesome");
    forum.addPost(post);
}

}

I will use your suggestions and see if that helps.