Have you ever heard of I/O? Let's learn what it is and how to use it! You probably know some simple math functions like But how does such a function differs from a real program?
The answer is simple: it does not interact with the real world in any way. We can call such a function for various arguments, and these arguments will be determined at compile time of our program. If we want to receive arguments at runtime or print the result to the user, we need a tool to help us do this. This is where input/output comes in handy. In this topic, you'll see how Scala implements I/O.
Printing to console
The first input/output function is print which will print a message inside the function to the console:
print("Hello, World!")
Note that we don't need to import anything to use the function.
The second function is println, which differs from the previous one only by a new line:
println("Hello, World!") // Writing "Hello, World!" to the console with the new line at the end
Alternatively:
print("Hello, World!" + "\n")
Instead of the String data type, you can use any other: Int, Float, Char, or Boolean:
println(5) // 5
println(0.12345) // 0.12345
println('a') // a
println(true) // true
In addition, we can print values and variables that are simple data types:
val a = 10
print(a) // 10Reading from console
Great! We got acquainted with the print function. Let's find out how you can read information from the user.
Scala provides us with a convenient function called readLine. To use it, you need to import this function. It is located in the scala.io.StdIn package. Let's do it:
import scala.io.StdIn.readLine
Now we can easily use the function:
val name = readLine()
In this example, we are trying to initialize a variable with a value we get from the user. If we enter Kate, the name value will be "Kate", if we enter Bob, then the value will be "Bob", respectively.
Be careful: the line break, in this case, will not go to the result. Everything that goes before the new line will be written to the variable.
While calling this function, the program will wait for your input in the console. We will get the result after pressing Enter:
scala> readLine()
// <- The program stops, the user enters "Alice Bella"
res0: String = Alice Bella
The function can also take a string that will be a hint for the user:
scala> readLine("Enter your name: ")
Enter your name: // <- The program stops, the user enters "Alice Bella"
res1: String = Alice Bella
This behavior will be the same with the following:
print("Enter your name: ")
val name = readLine()
As you can see, the code is more compact this way.
Note that calling the readLine function blocks other functions. Look at the following code:
val name = readLine("Enter your name: ")
print("Hi there!")
It will not print anything until we enter the name.
It may be useful to read just one string from the console instead of reading each word in separate values. Let's look at an example:
val friends = readLine("Enter the names of your friends: ").split(", ").toList
After reading the variable, we can work with it the same way as with ordinary variables. In this case, when entering the Alice, Bob, Alex string, we will get the following result:
List("Alice", "Bob", "Alex")Specific read
The readLine function reads a String from the user. If you enter 5, the variable assigned with this value will be "5".
Luckily, you don't need to convert a string to other data types because there are other useful functions:
readIntreads the Int data typereadLongreads the Long data typereadBooleanreads the Boolean data type
Note that these functions check the input value for validation. For example, in this case:
val result = readInt()
If you enter "hello", the function will throw a NumberFormatException because it was expecting a number, not a string.
A program with I/O
Let's write a simple Scala program with I/O that will use the user input in the output.
import scala.io.StdIn.readLine
@main def example =
val name = readLine("Enter your name: ")
println(s"Good morning, $name")Conclusion
In this topic, you got acquainted with the I/O in Scala. You learned that print and println allow you to print to the console, and readLine, readInt, readBoolean, readLong allow you to read data from the console. Now you can try and write your own program in Scala using I/O!