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

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