Standard input Standard input is a stream of data that goes into the program. By default, standard input gets data from the keyboard but it is possible to get it from a file. Kotlin functions Kotlin has a useful function to read data from the standard input. It is supported by the operating system. It is readline . It reads the whole line as a string. fun main () { val line = readLine ()!! println (line) } If you need to get a number from the input, you can use this construction: fun main () { val line = readLine ()?. toInt ()!! println (line) } To accept multiple words in a single use the following code val (a , b) = readLine ()!!. split ( ' ' ) println ( " $ a , $ b " ) val (c , d , e) = readLine ()!!. split ( ' ' ) println ( " $ c , $ d , $ e " ) Java Scanner Another way to obtain data from the standard input is to use the Java Scanner. Scanner allows a program to read values of different types (strings, numbers, etc) from ...
What is a variable? A variable is a storage for a value, which can be a string, a number, or something else. Every variable has a name (or an identifier) to distinguish it from other variables. You can access a value by the name of the variable. Declaring variables Kotlin provides two keywords val (for value) declares an immutable variable (just a named value or a constant ), which cannot be changed after is has been initialized. var (for variable) declares a mutable variable , which can be changed (as many times as needed) Both val and var keywords provide you a variable! To assign a certain value to a variable, we should use the assignment operator = It is also possible to declare and initialize a variable with the value of another variable. Storing different types of values There is one restriction for mutable variables (the ones declared with the keyword var), though. When reassigning their values, you can only use new values of the same type as the initial one. For implemen...
Integer Numbers If an integer value contains a lot of digits, we can add underscores to divide the digits into blocks to make this number more readable: for example, 1_000_000 is much easier to read than 1000000 You can add as many underscores as you would like e.g. 1___000_00 , 1_2_3 Underscores can't appear at the start or at the end of the number. If you write _10 or 100_ you will get an error. Characters A single character can represent a digit, a letter or another symbol. To write a single character we wrap a symbol in single quotes. A character cannot include two or more digits or letters because it represents a single symbol. e.g. ' abc ' and ' 543 ' are incorrect character literal. Strings Strings represent text information. So strings can include letters, digits, whitespaces and other characters. To write strings, we wrap characters in double quotes instead of single ones. A string can also contain just one single character like "A" . Do not c...
Comments
Post a Comment