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 trial

Java Java Objects (Retired) Delivering the MVP Wrapping up

Furkan Demirkan
Furkan Demirkan
7,926 Points

Error on my code and I dont know how to use string [] args?

I got this error from Wrapping up Challenge 4: "I expected to see the names I passed in through the args, but I do not. Hmm." I am not completely sure about what Im intended to do here. My code is the following. I suppose the error has something to do with Example.java

EDIT: I found an answer from another forum, and Im not entirely sure what how to use String [] args in this case. And I dont want to pass without actually comprehending it. I know alot about arrays from a few semesters in C, but Im not sure about the point of using it in this scenario. Can someone please elaborate?

Example.java
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("Swag", "Baller");
    // Add the author, title and description
    ForumPost post = new ForumPost(author,"The purpose of life...","GoT, pizza, and CS:GO");
    forum.addPost(post);
  }
}

1 Answer

Christopher Augg
Christopher Augg
21,223 Points

Hello Furkan,

You pretty much had this challenge in the bag. I will try and elaborate how the main args array works for you and show how it would be done in your code.

The args array in the main method is for command line arguments. Therefore, if I were using the console and ran the program, I would pass any number of strings when I did so. The program would then start and have those strings in the args array. I could then use the args array to access the strings for use in my program. For this particular program, you want to access them when you pass two strings during the instantiation of the User object. I have edited the code and provided it below. Please let me know if this helps or not.

Regards,

Chris

     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(args[0], args[1]);
         // Add the author, title and description
         ForumPost post = new ForumPost(author,"The purpose of life...","GoT, pizza, and CS:GO");
         forum.addPost(post);
       }
     }
Furkan Demirkan
Furkan Demirkan
7,926 Points

Thanks for the help! Much appreciated.