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;

int nAge = Integer.parseInt(sAge); //Convert the string to a number.``````
3
New Java Syntax, level 3, lesson 7
Locked
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));

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

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