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

Wayne Smith
Wayne Smith
12,526 Points

Can someone explain why my answer is not valid? It's proper java syntax from previous steps

The question is: Create a new variable called name and accept input from the console using the readLine method.

My code is: String name = console.readLine();

But it says there is a compiler issue, and points me to the dot and says readLine is not a method of the Console class (of which console is). I'm not new to coding just new to java and this exact code has worked in previous challenges. What's wrong with it?

Multiple.java
// I've imported java.io.Console for you.  It is stored in a variable called console for you.
String name = console.readLine();
Wayne Smith
Wayne Smith
12,526 Points

Nevermind. I changed it to

String name = console.readLine("Enter a name: ");

and it worked. Can someone explain why since that's not specified in the instructions? That method should run without placement text. Also, can someone explain why the treehouse compiler only accepts double quotes/" and not single quotes/'

There are two different methods of readLine as you can see here: https://docs.oracle.com/javase/8/docs/api/java/io/Console.html#readLine-java.lang.String-java.lang.Object...-

As you can see they return the same thing. The first one just provides a formated prompt.

Also ' and " are two different things in Java. ' is used for chars and " for strings. In JavaScript (and other languages) you can usually choose between ' and " (also if you usually prefer to use ') but in Java you have to stick to the rules.

1 Answer

String name = console.readLine("Any Question you might ever wanted to ask...");

More information: https://docs.oracle.com/javase/8/docs/api/java/io/Console.html#readLine-java.lang.String-java.lang.Object...-