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

Michael Bredthauer
Michael Bredthauer
13,429 Points

Problems with TreeStory Challenge Task 1, giving me compiler error that doesn't make sense.

I am on the challenge for Java TreeStory and the first challenge says to create a new variable that accepts input. I am using the String name = console.readLine(); and it gives me this compiler error that I don't understand. What is wrong with my code?

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

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Michael,

It's not so much your code, as it is a poorly worded question. The challenge assumes you will know that because the variable is called "name" you will ask for someone's name. Just add that text to the method call and your code will pass.

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

Keep Coding! :dizzy:

hey jason i have a sort of inappropriate question. i checked the readLine documentation "https://docs.oracle.com/javase/7/docs/api/java/io/Console.html#readLine%28%29" and the method as shown is not accepting parameters. can you please explain or elaborate. i'm just taking a interest.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Hi Tatenda Mushayakarara

That second definition doesn't, but the first one does. It takes a String to provide a formatted prompt.

readLine
public String readLine(String fmt,
              Object... args)
Provides a formatted prompt, then reads a single line of text from the console.
Parameters:
fmt - A format string as described in Format string syntax.
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification.

So, it can do both... provide a formatted string and capture the response, or it can just "read a single line of text from the console." This challenge in particular wants the first one.

I hope that clears it up. :)

Michael Bredthauer
Michael Bredthauer
13,429 Points

Im glad you asked that because that was curious to me, I passed the challenge which was a bit annoying since I did that before and it didn't work but whatever as long as I know that I am on the right track.

yeah it does..but i'm tempted to dive into that args type Object parameter.thanks