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 Java Basics Getting Started with Java IO

java code challeges

beginner coder, stuck on java basics code challenges..please help..

IO.java
// I have imported java.io.Console for you.  It is a variable called console.
public class Introductions {

    public static void main(String[] args) {

        Console console = System.console();

        String firstName = console.readLine("What is your name?");


            console.printf("Hello my name is %s\n", firstName);
            console.printf("%s is learning how to write Java programs\n", firstName);
              console.printf("%s loves to write Java programs because its fun!!", firstName); 
              //Error received -- how to resolve?
    }
}

I seem to have trouble with the format of the way i have to give these answers..would appreciate any help at all..

Alexandru Doru Grosu
Alexandru Doru Grosu
2,753 Points

The top comment says // I have imported java.io.Console for you. It is a variable called console.

The instructor means that you should pretend that the console object is already there.

So simply dropping out this line should fix your problem.

Console console = System.console();

This is the question:
Add a variable to store a user’s first name using console.readLine. Make sure the variable is the camel cased representation of "first name".

As per me this line should be the answer:

String firstName = console.readLine();

Is this correct? or do i have to add anything else?

That worked! Much appreciated

Hi..having another issue...i see you've completed this course...perhaps you could help.. This is the link to the question.. https://teamtreehouse.com/forum/running-program-in-eclipsegetting-nullpointerexception-errorplease-help

1 Answer

Alexandru Doru Grosu
Alexandru Doru Grosu
2,753 Points
String firstName = console.readLine();

Yes and no. In theory (if you read the Java documentation for console.readLine()) it should work, as the method also has this overload which accepts no parameters. However the tester they are using to determine whether or not your code passes the quiz, expects a different overload to be called (one where you pass a String argument to readLine).

So to make your code pass:

String firstName = console.readLine("your message for the user here");