Posts

Showing posts from July, 2020

Reading a multiline input

Reading multiline input may still be a bit tricky: we should take into account the position of a cursor and the reading methods behavior. Let’s investigate this process with an example: | This is a simple multiline input, that is being read |   is a position of a cursor before reading the input.  If we invoke next() method, the program will read the input till the whitespace, which is indicated by blue color: This |  is a simple multiline input, that is being read After invoking nextLine() method the program reads the whole line starting from the whitespace. This data is indicated by green color. nextLine() places the cursor at the beginning of a new line (if there is such a line in our input): This is a simple | multiline input, that is being read Then, let's invoke next() method two times. The first input is indicated by orange color. we may see that the position of the cursor is right after the word and before the whitespace: This  is a simple multiline| inp...

Scanning the input

The standard input is a stream of data going into a program. It is supported by the operating system. By default, the standard input obtains data from the keyboard input but it's possible to redirect it to a file. Actually, not all programs need to use the standard input. But we will often use it here to help you master our programming skills! The typical way to solve programming problems is the following: Read data from the standard input (System.in); Process data to obtain a result; Output the result to the standard output (System.out). Reading data with a scanner The simplest way to obtain data from the standard input is to use the standard class Scanner. It allows a program to read values of different types (string, numbers, etc) from the standard input. In this topic, we will consider reading data from the input. To use this class we should add the following import statement to the top of your file with the source code. import java.util.Scanner; Then we add the following const...