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

Java Basics question -- Call the printf function to print out a string in java.

For some reason this code won't pass the compiler. It may be a typo again, but Im not sure. Can somebody point me in the right direction?

import java.io.Console;

public class Name {
    public static void main (String[] args) {
        String firstName = "Chris";
        Console console = System.console();
        console.printf("%s can code in Java!",firstName);



2 Answers

28,558 Points

Your code is technically fine, but for most of the basic Java exercises on Treehouse the challenges are set up such that a lot of the boilerplate code is taken care of automatically. That includes creating a class and method to house your code and importing and instantiating the Console.

The only code you are meant to add on your own is the exact code the challenge requests you to add. So for this challenge only the code to declare and assign the name variable, and the code to print out that message is meant to be added.

Like this:

String firstName = "Chris";
console.printf("%s can code in Java!",firstName);

The above two lines are the only lines of code you need to enter to complete this task.

Thanks, worked perfectly!