Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Eleeza Amin
Eleeza Amin
34,496 Points

Java problems with Packages...I'm convinced I'm right...computer disagrees.

So... I was on the Packages Code Challenge on Java Data Structures and I was on task 4 and this was the question:

Now in the main method of Display.java, instantiate a new BlogPost. Use System.out.printf to print out "This is a blog post: %s" and pass in the newly created BlogPost object to replace the string formatter.

I did it, just like in the video and I got some compiler errors:

./Display.java:4: error: illegal start of expression
    import com.example.BlogPost;
    ^
./Display.java:4: error: not a statement
    import com.example.BlogPost;
                      ^
./Display.java:5: error: cannot find symbol
    BlogPost blog = new BlogPost();
    ^
  symbol:   class BlogPost
  location: class Display
./Display.java:5: error: cannot find symbol
    BlogPost blog = new BlogPost();
                        ^
  symbol:   class BlogPost
  location: class Display
4 errors

I literally copied what the video said, and I got the same thing. I looked it up on the forum but there were no posts on this so...do I have a problem?

Here's my code: Display.java

public class Display {
  public static void main(String[] args) {
    // Your code here...
    import com.example.BlogPost;
    BlogPost blog = new BlogPost();
    System.out.printf("This is a blog post: %s %n", blog);

  }
}

If anyone knows the answer to the problem that would be awesome.

Thanks Treehouse! ~ Eleeza

1 Answer

Your import statement needs to be at the top of the file, outside of the class.

import com.example.BlogPost;

public class Display {
  public static void main(String[] args) {
    // Your code here...
    BlogPost blog = new BlogPost();
    System.out.printf("This is a blog post: %s %n", blog);
  }
}
Eleeza Amin
Eleeza Amin
34,496 Points

Hi, thanks for the response, but it didn't work for me. I don't know if this is a technical issue with my computer or something...maybe?

I got 3 out of the 4 errors again:

./Display.java:1: error: BlogPost is not public in com.example; cannot be accessed from outside package import com.example.BlogPost; ^ ./Display.java:6: error: cannot find symbol BlogPost blog = new BlogPost(); ^ symbol: class BlogPost location: class Display ./Display.java:6: error: cannot find symbol BlogPost blog = new BlogPost(); ^ symbol: class BlogPost location: class Display 3 errors

But anyway, thanks for responding :)

Looking at the error, I'd go back to the BlogPost file and make sure that you are declaring the class as public.

package com.example;

public class BlogPost {

}
Eleeza Amin
Eleeza Amin
34,496 Points

It works now! Thanks so much :)