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 Basics Getting Started with Java Strings, Variables, and Formatting

Defining a variable after object created with no visibility to code

Hi. I've wasted a lot of time, searched this forum, stack overflow, google, etc and am wondering if there is a problem with the site or activity... No combination seems to be right. The closest I come to minimizing my errors in the editor is when I call the class "Name" which is the name of the file ( "name.java" ). This is frustrating. I'm new, have specified that I'm a beginner, and feel like some the assumption dependent on advancement here is well beyond "BEGINNER." Ridiculous tbh

Name.java
// I have setup a java.io.Console object for you named console
public class Name {
      public static void main(String[] args) {
      Console console = system.console();
        String firstName = "Chris";
        console.printf("Hello, my name is Chris\n");

      }

}

2 Answers

Simon Coates
Simon Coates
28,694 Points

try

    //  Console console = system.console();
        String firstName = "Chris";
        console.printf("Hello, my name is %s", firstName);

The important information is here (at least), it doesn't require that you enter the class and method code that would ordinarily be required to run. Beyond this, there's an assumption that the console object is already available (having used some code similar to yours). As such, i commented out that line (though i think there's a case issue with this line - maybe System.console() - though i could be wrong). The treehouse forum is able to be searched from google, if you enter a snippet of the question (maybe between "" to get a precise match). Unless, it's an especially new course or an unpopular course, there will usually be other people who struggled and code available (see https://teamtreehouse.com/community/code-challenge:3492 )

Simon Coates
Simon Coates
28,694 Points

You may hit real issues with the test verification, where it's flawed and giving you the wrong message or where the code you enter runs in a context it doesn't tell you about properly (it may have provided a variable/import statement or two, and run your code in a class it doesn't show you). It might be some small comfort that your code was on some level more advanced than what it wanted.

Crud! I could have sworn I tested this very early on (can't say for sure because I did dozens & dozens of tests), but

String firstName = "Chris"; console.printf("Hello, my name is %s", firstName);

Definitely worked. Thanks so much Simon for taking time to look at this!!