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 Using your New Tools Multiple Format Strings

Name console.readLine

String name = console.readLine(); isn5 correct, what's being ask3d of me?

String name = console.readLine();

1 Answer

Simon Coates
Simon Coates
8,177 Points

I think it's a flaw with the test code. The explanation for what's going wrong is complicated. But avoiding offering an explanation altogether seemed unsatisfactory. It's not your fault. It's not something that you could have deciphered. YOUR LINE OF CODE WAS FINE. For instance, I was able to run it successfully in the following program on an online sandbox.

import java.util.*;
import java.io.Console;

public class SayHi
    public static void main(String[] args)
        Console console = System.console();
        String name = console.readLine();
        System.out.println("Hi "+name);

The real console has a method called readLine() that accepts no parameters (see https://docs.oracle.com/javase/8/docs/api/java/io/Console.html#readLine-- ). However, their version of it (called console, but seemingly an instance of a class called MockConsole) seems to require a parameter (i.e. it doesn't have the full set of method overloads).

The problem will disappear if you provide a parameter.

String name = console.readLine("Can I get a value?");