There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Usually when we’ve been creating objects with our constructor methods, we’ve named it and defined it on the same line, for example:
Student james = new Student(19, "English");
but just like variables, we don’t need to define them right away. We can do it on two lines:
Student james;
james = new Student(19, "English")'
So since we don’t know what Strings exactly feed is going to contain yet, we’re merely putting it out there that there is going to be a new object for Newsfeed. Then depending on which if code block runs, we fill in the new object with its info later.
If we can understand the ‘if, else if, and else’ statements running their respective line of code depending on what the argument is typed in the terminal, we can trace back to the information that is passed up in this entire code.
For example:
Terminal input: java Newsfeed Human
If we start reading our code at the main method, we can trace back this line of code:
feed = new Newsfeed(humanTopics);
This line uses ‘humanTopics’ as an argument, which is then passed to the constructor:
public Newsfeed(String initialTopics) {
topics = initialTopics;
}
This makes ‘humanTopics’ into ‘topics’:
topics = initialTopics;
which traces back to the Newsfeed class:
String topics;
meaning that to print the array that we choose in the terminal (Human) in the ‘if else’ lines of code, we have to use the ‘feed’ object along with the code ‘topics’ (feed.topics). Otherwise, if we experiment with this, we can try using (feed.humanTopics) instead and go straight to printing it right out of the ‘if else’ statement. The problem with doing this is that not only do we get an error, but logically we are always going to have the ‘humanTopics’ array printed out regardless if we input java Newsfeed Robot in the terminal. I suggest experimenting with this in a notepad with this java class and terminal because Codecademy in this module saves your progress in a manner that might hinder anyone to continuously experiment outside the module curriculum.
I hope this makes it clear for every one and thank you for helping me understand your response about the ‘Newsfeed feed;’.
You may refer her answer -> @tashachetty241033936.
And also, I still have a bit problem running in the terminal after doing @tashachetty241033936 instruction. The problem is I can’t seem to print it out.
$ javac Newsfeed.java
Newsfeed.java:31: error: cannot find symbol
System.out.println(Arrays.toString(feed.topics));
^
symbol: variable feed
location: class Newsfeed
Soon after, I finally figure out that, I need to put the ‘Newsfeed feed;’ right below the ‘public class Newsfeed{}’. I don’t know why though.
Hope this helps anyone that came across the same problem as mine.
Have a nice day
P/S: Why do we need to put it under class Newsfeed? Can’t we just put it under main() method?Are there any differences from doing that? If anyone could tell me, I would be so appreciated!
public class HelloWorld() {
// This comment is inside the HelloWorld class but is outside of the main method
public static void main(String[] args) { // This method is inside the HelloWorld class
System.out.println("Hello world!");
}
}
In this exercise, it was said to run the file from passing argument but not mentioned to compile before running in the terminal. To be honest, if you are a beginner, you will ask yourself like,
wait whats happening here, I followed the instructions, but it seems error, why?
I would like to draw attention for that to add this information for beginners: CommunityBug Reporting
Thank you for clearing confusion but I am talking about the specific moment of the problem. If the little info was added, coding could have been easier though! Thanks, Anyways!
So I realize leaving the argument empty will not trigger else program.
Is there any way we could interact with blank input?
Like for example, if the user leaves the argument blank, assign string “empty” to the argument.
So it will trigger the else
.equals() is a method that compares two strings. If the value of the two strings are equal, it returns the boolean true. Otherwise, it returns false. It is used like so: