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 trialchrisfinelli
1,161 Points2 questions: the reason for my compiler failure & where is Main.java's constructor?
Hi! I feel like I've searched the community & net almost exhaustively and am still missing some small detail to pass the step 4 of the task (you'll notice couple of statements void as result of my experimentation...) I get this when try to compile which also has lead to a couple of other questions...
"/Main.java:25: error: cannot find symbol ForumPost post = new ForumPost(author, title, description); ^ symbol: variable title location: class Main ./Main.java:25: error: cannot find symbol ForumPost post = new ForumPost(author, title, description); ^ symbol: variable description location: class Main Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 2 errors"
1) Is there no need for a constructor in the class with the main method or is the fact that this class' name is "Main" create a special case?
2) can we initialize variables without instantiating objects? I thought this was not possible...
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;
}
// Uncomment this when you are prompted to do so
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()
);
}
}
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.lastName = lastName;
this.firstName = firstName;
}
// TODO: add getters for firstName and lastName
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
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;
}
}
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);
}
// Uncomment this when prompted
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]);
// String title = new title();
// String description = new description();
// 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);
}
}
michaelcodes
5,604 PointsJust to add to Patrick, Main is the constructor for that class (and the program), It is the first thing that runs and as he said can take parameters. These parameters however must be an array of Strings called args, if theyre specified at all. You can chose not to use it (in your own programs, not this code challenge.. the challenge requires it :)
3 Answers
Patrick Bluth
13,285 PointsHi again,
Technically no, that print statement does nothing in terms of setting the values of args[0] and args[1]. The statement there is simply to inform the user that main takes two inputs, where the first one corresponds to first name and the second to last name. However, the System.exit line in that if statement forces the program to stop if firsrName and lastName are not inputed, and by doing so it forces the user to input those two variables to run the program. So in that sense it does set those variables in place. The program would simply crash if that error handling wasn't in place and main wasn't passed in two variables.
We know that main takes these two variables based on the fact that the main creates an author object using args[0] and args [1]. Because the author takes two arguments (firstName, lastName), args[0] and args[1] should therefore correspond to firstName and lastName, and these values are determined by what is past into the main method call.
And to make sure you understand, when you first would run the program, this is when you would pass in thr fiestName and lastName variables via the console. These are not declared anywhere in the code and are given by the user.
I hope that makes it more clear. Let me know if you have any more questions.
chrisfinelli
1,161 PointsHello and thank you both for responding. I think I've been getting caught up in the semantics... Greatly appreciate you both chiming in!
chrisfinelli
1,161 Pointsoh... admittedly, it's not clear to me where we loaded the values of the main method's args array. Did the System.out.println("Usage: java Main <first name> <last name>");
specifically set args[0] as firstName & args[1] as lastName?
chrisfinelli
1,161 PointsVery good, Patrick. Thanks so much for all of your help!
Patrick Bluth
13,285 PointsPatrick Bluth
13,285 PointsYou are right, there is no constructor made for main, however, main can take in arguments. This is where the main(String args[]) comes from (it can take an array of strings).
I've also completed the challenge, and you can have a look at my main class here
The issue you are having is because you are trying to create a new ForumPost object, which takes a String title and a String description. However, you are passing in title and description, which have not yet been declared as Strings. You are supposed to either declare them outside and then pass them in, or just pass in a string like I did. Once these variables are passed into the new object, they will then be initialized through the Constructor you created in the ForumPost class.