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 trialAdiv Abramson
6,919 PointsI don't understand why the console object doesn't have a readLine() method.
Not sure what's wrong here. Should be an exceedingly simple task. However I'm obviously making an error. Just don't know what it is.
// I've imported java.io.Console for you. It is stored in a variable called console for you.
String name = console.readLine();
2 Answers
Andrew Antwine
1,412 PointsString firstName = console.readLine("What is your name? ");
When you are creating a String variable, you must have the two quotation marks. :) I know this command as a way of the computer asking your some type of defined input. If you get a number back as an answer, you must turn it into an int using the parse command.
Hope this helps!
Craig Dennis
Treehouse TeacherAdiv,
That Mock Console is my bad, I've written a fake, or mock, console
that I use to test what has been written. The actual java.io.Console
does take a no parameter version, so I made a mistake. Sorry about that! I will fix it so others do not run into the same problem.
In Java single quotes are used to declare a single character, and double quotes for a string like so:
char aLetter = 'H';
String someLetters = "Hello";
We go into that in Java Objects.
Thanks for the feedback!
Adiv Abramson
6,919 PointsThank you so much. That really clarifies matters. I must keep the distinction between single and double quotes vis-a-vis Java coding in mind! Perhaps the confusion arises from attempting to study more than one language concurrently. I'm taking the Javascript and Python courses here and also working on Perl independently (but hoping Treehouse will <hint>add a course in Perl someday. </hint>). Since most entry level programming jobs these days seem to require knowledge and experience in up to 15 different languages and technologies, confusion over syntax and style in each language is an ineluctable consequence of trying to master more than one language at a time, I'm afraid.
Great course, BTW. :-)
Adiv Abramson
6,919 PointsAdiv Abramson
6,919 PointsThank you very much for your assistance. I see two things from this exercise:
1.) the readLine() method requires some text as an argument; it can't be invoked with an empty argument list. console.readLine(); is missing an argument and so it won't work, 2.) Java is particular about double vs. single quotes. I was under the impression that, like Javascript, both types of quotes could be used interchangeably, so long as they are paired. I got flagged for errors when I enclosed the prompt with single quotes.
Why do compiler authors insist on writing such inscrutable and arcane error messages? I would have understood what was wrong if it had said something like "console.readLine() requires a string argument surrounded by double quotes, e.g. console.readLine("Prompt the user for something here: ")" Instead it mentioned something about the readLine method not being applicable to Mock Console and that a String or Object was expected.