FAQ: Learn Java: Arrays - String[] args

This community-built FAQ covers the “String args” exercise from the lesson “Learn Java: Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise String[] args

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 (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 (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 (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Here in line #12
Newsfeed feed;
What we are doing here? Can anyone please expalain it

3 Likes

I was confused at first too but then it clicked.

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.

9 Likes

Why does line 32 contain feed.topics? Why reference topics here?

because it use ‘String array’ in class Newsfeed

Hello,

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;’.

Respectfully,

Reddzl J Tuano

1 Like

After completing the instruction for this exercise I ran the code and typed in the terminal

java Newsfeed Robot

But I all I got was

Error: Could not find or load main class Newsfeed
Caused by: java.lang.ClassNotFoundException: Newsfeed

I don’t know why this comes up.
Please help.

Icdrasyl

2 Likes

Hi icdrasyl,

You will have to first compile the java class in bash using the following command:
javac Newsfeed.java

Thereafter you will be able to execute the following:
java Newsfeed Robot
java Newsfeed Human

4 Likes

Hi Everyone,
I dont understand the basic idea of this part. Can some explain this part.
Thanks
:slight_smile:

Why it doesn’t run when i run the Newsfeed on terminal without an arguement?

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 :slight_smile:

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? :confused: If anyone could tell me, I would be so appreciated!

The main method is in the Newsfeed class.

Here's an example...
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: Community Bug Reporting

There were exercises on how to compile and run files from the terminal in the very first lesson. I’ve attached them below for your convenience.

1 Like

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

Thank you in advance!

Can someone please explain to me, “.equals.” It comes out of nowhere in this exercise with no previous introduction.

Welcome to the forums!

.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:

string1.equals(string2)

Earlier in the Java course, it does teach the .equals concept. I think it is in String concatenation. Try going back to see if you can find it.

1 Like

Would this if-else statement work as a switch statement?
Would it be better?
How?