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 trialWilliam Richert
6,418 PointsStumped on passing final code challenge
I cannot see what I am missing here, suffice to say there are error messages pointing me to the parameters that I am passing into the new User constructor call.
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
String firstName = args[0];
String lastName = args[1];
User author = new User(String firstName, String lastName);
ForumPost post = new ForumPost(User author, String "Java Rocks!", String "Onwards!");
forum.addPost(post);
}
}
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;
}
// TODO: We need to expose the description
public String getDescription() {
return mDescription;
}
}
public class User {
private String mFirstName;
private String mLastName;
public User(String firstName, String lastName) {
// TODO: Set the private fields here
this.mFirstName = firstName;
this.mLastName = lastName;
}
public String getFirstName() {
return this.mFirstName;
}
public String getLastName() {
return this.mLastName;
}
}
public class Forum {
private String mTopic;
public String getTopic() {
return mTopic;
}
public Forum (String topic) {
mTopic = topic;
}
public void addPost(ForumPost post) {
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
String firstName = args[0];
String lastName = args[1];
User author = new User(String firstName, String lastName);
ForumPost post = new ForumPost(User author, String "Java Rocks!", String "Onwards!");
forum.addPost(post);
}
}
1 Answer
Ken Alger
Treehouse TeacherWilliam;
In Example.java
you have multiple errors, although similar and easy to fix.
In these two lines...
User author = new User(String firstName, String lastName);
ForumPost post = new ForumPost(User author, String "Java Rocks!", String "Onwards!");
You don't need to pass in they data types here, just the variable names. If, for example firstName
is not of type String
Java will let you know. Removing the type declarations will get your code to pass.
Happy coding,
Ken
William Richert
6,418 PointsWilliam Richert
6,418 PointsThank you very much. I had a real misconception about adding the datatypes into method and constructor calls.
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherWilliam;
Yes, when you call methods, or create new objects you typically don't want to include the types. There are cases in which you would but one of the things that I personally like about Java is that it is strongly typed and knows that when you create a new
User
, in this case, that you need to pass in aString
andString
. If you attempt to pass in something else, anint
, for example, it won't like that.Happy coding,
Ken