JavaRush /Courses /New Java Syntax /Keyboard input

Keyboard input

New Java Syntax
Level 2, Lesson 7
Available

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

Comments (8)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Alex Level 5
21 April 2024
I assume this is the point where we suppose to upgrade our subscription, to get a better explanation 🫤
31 January 2023
I didn't get anything at all!!!!
6 January 2023
Very bad, java rush. It is not enough or to much explanation. If I must only to remember it, the explanations are excess
ValeriyF Level 10
17 December 2022
Cool lesson with no explanation
Михаил Level 3
27 October 2022
Ок. I will go to Google to search for information and figure it out on my own. Thank you for pumping my skills. UPD: Im figured it out. tip: don't try to figure it out now, just remember it as a whole, and you will figure it out in the future. "BufferedReader reader = new BufferedReader(new InputStreamReader(System.in ));" and then: String name1 = reader.readLine(); *// the first line of input String name2 = reader.readLine(); *// second line of input etc
Gerasim Level 2
31 August 2022
Why suddenly got so complicated? With no proper explanation
tokyoocean Level 11
5 July 2022
this course is just frustrating. especially if compared to "syntax pro" one. 😖😖😖
nomadcoder Level 3
10 June 2022
Not enough details in this lesson and explanation. Example here doesn't help to solve problems in next lessons. Need more details on BufferedReader. What are these two line doing in example 1: InputStream inputStream = System.in; Reader inputStreamReader = new InputStreamReader(inputStream);? What are this line mean in example 3, Scanner scanner = new Scanner(System.in);? What are InputStream and what are Scanner? On example 2, why do you need real nAge as string and parse it to int when you can just read it as int like in example 3?