CodeGym /Courses /Java Syntax /Keyboard input

Keyboard input

Java Syntax
Level 3 , Lesson 7
Available

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


"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 System.in."

"Sounds easy."

"But System.in 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 = System.in;
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.
3
Task
New Java Syntax, level 3, lesson 7
Locked
Good or bad?
Good or bad?
Example 2
A more compact version of the previous example:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

String name = reader.readLine();
String sAge = reader.readLine();
int nAge = Integer.parseInt(sAge);
Example 3
Even more compact
Scanner scanner = new Scanner(System.in);
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, System.in."

"But System.in 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."

3
Task
New Java Syntax, level 3, lesson 7
Locked
Interval
Interval
3
Task
New Java Syntax, level 3, lesson 7
Locked
Positive and negative numbers
Positive and negative numbers
3
Task
New Java Syntax, level 3, lesson 7
Locked
Day of the week
Day of the week
Comments (112)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Kelvin Level 14, Lagos, Nigeria
3 May 2025
Incase you do not understand let me try and explain it in a way that might be easier to understand. At this point we all know System.out.println() is for displaying stuff. Now in order to get stuff we can use System.in.read(), but there is an issue with this, the issue is that System.in.read() only takes in a single character at a time. what do i mean by this? When ever you have System.in.read in your code, all it recognizes is a character(char) and thats not all if you type in "A" for example, it doesnt store "A" exactly it stores the ASCII value of A. let me show you an example int name = System.in.read(); System.out.println(name); if i run this code and then type in "kelvin", what would be recognised is the first letter "k" the rest is disposed and it wont even display "k" because when you typed in that "k" what was saved was 107. if you notice my name variable is of type int because System.in.read() converts whatever you enter into the ASCII representation. I hope this makes sense so far. Now if i want to save my full name i would har to do this System.in.read(); System.in.read(); System.in.read(); System.in.read(); System.in.read(); System.in.read() so each of them will hold the ASCII representation of K-E-L-V-I-N which will be 75-69-76-86-73-78 . I hope i am not getting you confused, it took my a while to wrap my head around this also so now you see that we cant directly do anything usefull with System.out.in, That is where InputStreamReader() comes in. Since System.in.read() only gives us a single character (in ASCII), it's not very useful for real-world inputs like names, sentences, etc. That's why Java gives us InputStreamReader, which takes System.in and converts the ASCII into readable characters. What do i mean by this, if i click "a" System.in.read recognises it as 97, what inputStreamReader does is it takes this ASCII value and converts it into what we understand which is "a"
Jimmy Level 31, Lausanne, Switzerland
9 November 2024
stay hard, keep going
Anonymous #11549069 Level 10, Katowice, Poland Expert
11 August 2024
you should not give film about Scanner if you want to do BufferedReader in next lesson test
Anonymous #11513059 Level 3, Ognjaci, North Macedonia
8 May 2024
I like the explanation of packages
Anonymous #11413956 Level 3, Mesa, United States
24 October 2023
This section is very difficult for me..
Be happy Level 2, United States of America, United States
29 May 2024
I must agree it is not explained well there is no primer to prepare you
BackwardsPi314 Level 6, San Francisco, United States
30 July 2024
Same here! But I think copy pasting the scanner part is easiest until you memorize what to write. Just remember the import java.util.scanner
Anonymous #11274451 Level 3, United States of America, United States
3 June 2023
This instructor is very confusing
Anonymous #11263139 Level 7, Mexico
28 January 2023
The article here https://codegym.cc/groups/posts/4-reading-from-keyboard
Khrysus Level 3, Denver, United States
12 December 2023
Thank you lmao
Lunita Level 4, Dominican Republic
16 January 2023
I'm curious as to why using all these verbose is better than just using Scanner… Why?
Lunita Level 4, Dominican Republic
16 January 2023
Alright Google already answered: The Scanner has a little buffer (1KB char buffer) as opposed to the BufferedReader (8KB byte buffer). BufferedReader is a bit faster as compared to Scanner because the Scanner does the parsing of input data and BufferedReader simply reads a sequence of characters. Another difference is Scanner is newer than BufferedReader. This means, you have access to BufferedReader in almost all JDK versions, but Scanner is only available after Java 5. BufferedReader is synchronous while Scanner is not.
Khrysus Level 3, Denver, United States
12 December 2023
Thanks for this! Really helpful
KrisP Level 4, Barcelona, Spain
15 January 2023
Thank you for the video. Very explicit :)
Anonymous #11149518 Level 5, United States of America, United States
29 October 2022
I love the addition of videos. 😊