Keyboard input

"Amigo, your time has come. I'm now going to tell you about keyboard input."

"We've used System.out to display data on the screen. To receive input, we'll use"

"Sounds easy."

"But has one shortcoming – it only lets us read character codes from the keyboard. To get around this problem and read big chunks of data all at once, we'll use a more complex construct:"

Example 1
Input a string and number from the keyboard
InputStream inputStream =;
Reader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String name = bufferedReader.readLine(); //Read a string from the keyboard
String sAge = bufferedReader.readLine(); //Read a string from the keyboard
int nAge = Integer.parseInt(sAge); //Convert the string to a number.
Java Syntax,  level 3lesson 7
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and earn a little dark matter. What's more, you combat laziness.
Example 2
A more compact version of the previous example:
BufferedReader reader = new BufferedReader(new InputStreamReader(;

String name = reader.readLine();
String sAge = reader.readLine();
int nAge = Integer.parseInt(sAge);
Example 3
Even more compact
Scanner scanner = new Scanner(;
String name = scanner.nextLine();
int age = scanner.nextInt();

"Any questions?"

"Uh...I didn't understand anything."

"To read a string from the keyboard, it's most convenient to use a BufferedReader object. But to do that you have to pass in the object you're going to read data from. In this case,"

"But and BufferedReader are incompatible, so we use another adapter – another InputStreamReader object."

"I think I get it now. What is this Scanner class?"

"Scanner can be convenient, but it's not very useful. The thing is, as you proceed (both in studying and working), you'll use BufferedReader and InputStreamReader often, but Scanner – very rarely. It's convenient in our example, but in the future it won't be useful very often. So we won't use it much."

"That seems clear, but I'm not sure I understood everything."

Comments (67)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Level 9 , Kampala, Uganda
10 March, 16:04
Is there an android app which works like intellij idea?
John Squirrels Website Admin at CodeGym
11 March, 08:49
AIDE, for example.
Level 8 , Moscow, Russian Federation
6 March, 11:35
I still don't understand what an object is and why it is needed. And I also don't understand how many types of variables there are. In the last example, we create a variable of the type "BufferedReader" with the name "reader", where did we get this type of variable, came up with? And why exactly this type?
Level 4 , Istanbul, Turkey
16 March, 08:07
I also didn't understand what is reader
Shashikant Sharma
Level 4 , Surat, India
8 June, 01:51
Object is nothing but a variable which stores data or reference. Mainly, there are two types of data: Primitive and Composite. An in the last example, We did not create a "BufferedReader" variable, you misunderstood it. "BufferedReader" is a built-in class in package. We used it to create an object called "Reader" and you might we wondering how did we got "Reader" object. So to that, in previous example we created class "BufferedReader" and created its object as "bufferedReader" but to make it short, we created "Reader" object as we can name anything to object other than keywords. So, to make it we made it precise. HOPE YOU UNDERSTOOD.🙂
Level 5 , Pune, India
6 January, 04:19
I feel some more details should be provided
Haj Adib
Level 5 , Tehran, Iran, Islamic Republic of
27 November 2020, 21:00
I liked the compact version of readers.. So cool :)
Marco Festi
Level 8 , Parma, Not in list
14 October 2020, 14:14
Hello everyone, shouldn't the code in example 1:
Reader inputStreamReader = new InputStreamReader(inputStream);
be something like:
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
Derya A.
Level 4 , York, United Kingdom
24 January, 13:44
Hello. inputStreamReader variable is Reader type, because InputStreamReader class extends abstract class Reader. ( Therefore it is correct to create new InputStreamReader object with Reader type. On the other hand, your suggestion is also correct. But when you build large programs, it is convenient to use the first instance. You'll understand why once you study Object-Oriented Programming principles:)
Agustin Jauregui
Level 22 , Auckland, New Zealand
22 July 2020, 21:41
When "Amigo" says "I didn't understand anything" I can totally relate to that. I hope to come back to this comment when reaching higher levels and hopefully I'll find this easier :)
Drazen Jankovic
Level 10 , München, Germany
27 October 2020, 09:22
And, now you're at level 22... How does it seems right now? 😀
Agustin Jauregui
Level 22 , Auckland, New Zealand
6 November 2020, 22:26
Haha I have to admit that this explanation feels like a lot of new info at level 3 but now I have a better understanding of what's going on. One of the best takeaways from this lesson is "get used to using BufferedReader instead of Scanner". You'll see a lot of people on the help section not doing it >.<
Level 22 , Die, France
30 June 2020, 07:14
Why Scanner is not very useful?
Agent Smith
Level 38
9 August 2020, 16:37
Rishabh Joshi
Level 9 , Indore, India
25 November 2020, 08:41
when did you leave the matrix? :O
Brandon Leirer
Level 7 , Keller, United States
13 May 2020, 19:45
BufferedReader reader = new BufferedReader(new InputStreamReader(; Can somebody break down this line for me? It looks like "BufferedReader" variable is declared and assigned the name "reader" and then assigned to the newly created "BufferReader" object, which is itself simultaneously assigned to the newly created "InputStreamReader" object that is referenced to "" . Is that correct? I am getting a bit confused on the nesting relationships.
Level 4 , Yerevan, Armenia
6 June 2020, 08:46
1st of all nice, you wrote this on my birthday XD 2nd. ok so BufferedReader reader = new BufferedReader(new InputStreamReader(; 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1. that is the brain of everything 2. we give that brain a name 3. then we declare something new to that brain 4. it reads the input from the keyboard(what you type) 5.its the opposite of system.out(it inputs data, doesn't output) hope this helps
Rahul Mahadev
Level 4 , Cambridge, United States
10 May 2020, 02:03
What does paresInt do ?
Level 41 , Crefeld, Germany
12 May 2020, 15:43
The parseInt method converts a string to an int. In the above example String sAge = reader.readLine(); stores a string in the variable sAge. The next line int nAge = Integer.parseInt(sAge); converts that string into an int and stores it in nAge. You could now perform further calculations with the int variable, if you needed to. :-)
Level 6 , Delhi, India
6 May 2020, 19:19
May kindly elaborate the 2nd line of example 1.