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 Receiving Input

Ocky Wiemeijer
Ocky Wiemeijer
5,451 Points

Why is there no need to call a String variable that is declared as 'console.readLine()'?

I'm a little confused, but maybe i'm missing something. In other languages you first declare a variable, and then, for it to appear in the console or in the view, you have to call it somehow, right?

In the video it just says:

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

and 'What is your name?' automatically appears in the console once you compile and run it.. The variable 'firstName' isn't called explicitly. Or is it because it is referred to with a '%s' in the console.printf statements?

5 Answers

Not quite, no.

The prompt is the string ""What is your name?: ". Let's understand what is happening here. The computer is asking the user a question; "What is your name?:". The user answers the question by typing "Steve", if they're me. The value "Steve" gets stored in the variable named firstName. The prompt and the variable firstName are wholly unrelated. The prompt is just a string - it isn't stored anywhere, it is just used once for its single purpose. The variable is stored for later use in the code.

The console method readLine has the ability to prompt the user - this is key as it halts execution pending a response. If there were no prompt we'd sit here staring at the screen wondering what was going on! The string variable firstName has not become a prompt, no. The string "Type anything here" has become the prompt but, thankfully, the programmer made it a sensible prompt; "What is your name?:".

How to control when the prompt appears? Control when you deploy the readLine request. The prompt goes hand-in-hand with that due to the execution pause, as above.

HI there,

The console method readLine uses a string as a prompt to let the user know their input is required. Surrounding the characters with double quotes is enough to identify the required output as a String; the value of that string isn't required later in the code so there is no nd to assign the string's value to anything.

In your example, once the user has entered somethign in response to the prompt, hopefully their name!, that input will be stored in the variable firstName for use later in the code.

So, the console method can take a string to be output to the console. I am sure you could create a string variable outputString prior to the line of code you mention and say something like:

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

// but why not just

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

I've never tried that but it might work! But there's no need to - the output string is throwaway code, is isn't required as a variable so it can be used as a string literal surrounded by quotes. It is then discarded once used, saving resources.

I hope that answered your question.

Steve.

Ocky Wiemeijer
Ocky Wiemeijer
5,451 Points

Thanks for your quick reply!

So basically, the String variable firstName becomes a prompt once it is declared as the console method readLine, and automatically gets called and 'printed' to the console as a prompt?

Maybe my question should be, how can you control when the prompt is displayed in the console? Because now, it is displayed as soon as it is declared.

Ocky Wiemeijer
Ocky Wiemeijer
5,451 Points

Crystal clear now! Thanks!

No problem - glad to help.

What is your name?: ... Steve :-)

No problem - and thanks for the follow on Twitter! :smile: :+1: