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 trialGaspar Santiago
3,377 PointsBummer! 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.
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
}
public class User {
public User(String firstName, String lastName) {
// TODO: Set the private fields here
}
}
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());
*/
}
}
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
28,558 PointsThe 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
3,377 PointsHere'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.
Gaspar Santiago
3,377 PointsGaspar Santiago
3,377 PointsI tried that but that throws an ArrayIndexOutOfBoundsException.
andren
28,558 Pointsandren
28,558 PointsAre 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:
User.java:
ForumPost.java:
Example.java:
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.