CodeGym /Java Course /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?
Write the compare(int a) method so that it: - displays "The number is less than 5" if the method argument is less than 5, - otherwise, displays "The number is equal or greater than 5".
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
Implement the checkInterval method. The method should check whether an integer is between 50 and 100, not inclusive, and report the result on the screen in the following form: "The number a is not in the interval." or "The number a is in the interval.", where a is the method argument. Example for 11
3
Task
New Java Syntax, level 3, lesson 7
Locked
Positive and negative numbers
Use the keyboard to enter a number. If the number is positive, then double it. If the number is negative, add one. If the entered number is zero, display zero. Display the result on the screen.
3
Task
New Java Syntax, level 3, lesson 7
Locked
Day of the week
Use the keyboard to enter a number representing a day of the week. Then, depending on the entered number, display the name of the day of the week: "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", if you enter a number greater than 7 or less than 1, display "No such day o
Comments (111)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Jimmy Level 3, Lausanne, Switzerland
9 November 2024
stay hard, keep going
Anonymous #11549069 Level 5, 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 22, United States of America, United States
29 October 2022
I love the addition of videos. 😊
Amber Singh Rathour Pandey Level 8, Delhi, India
9 October 2022
What does InputStream do? It is present in Ex.1, but not in the second one. Thanks.