Methods of Printing a Line in the Scala Programming Language
Methods of Printing a Line in the Scala Programming Language in Ubuntu 20.04.
Method # 1: Using the “println” Command
The “println” command in the Scala programming language is used to print a line while introducing a new line at the end. In this way, if you want to print more than one line, each of them will be printed on a separate line. You can see the following Scala script for learning how to use the “println” statement:
In this program, we just intended to print two messages on the terminal on separate lines. That is why within our “main()” function declared inside our driver class, we have used the “println” statement twice for printing both of these messages on the terminal.
You can execute the command shown below to compile this script:
Then, you can execute the following command for running this script:
The image shown below depicts the output of this script. From this output, you can verify that our “println” statement has nicely printed the two messages on separate lines.
Method # 2: Using the “print” Command
With the “print” command of the Scala programming language, you can print as many lines as you want, however, no new line will be introduced in the output. The following Scala script shows the usage of the “print” command:
In this example, again we wanted to print two different messages on the terminal. However, this time, we have used the simple “print” command to do so. It means that both of our messages will be printed together on the same line without any space and hence, we can expect our output to look messy this time.
The output of the “print” command is shown in the image below:
However, we can still make our output look decent while using the “print” command. For doing so, we will need to modify the above Scala script slightly in the following manner:
In this Scala script, we have simply introduced the escape sequence “\n” after every “print” statement for the messages that we wanted to print on the terminal. This escape sequence is used to introduce a new line wherever it is used.
When we executed this modified script, our output turned out to be exactly the same as it was with the “println” statement.
Method # 3: Using the “printf” Command
The “printf” command of the Scala programming language is used to print format strings on the terminal. To use this command, you can take a look at the following Scala script:
In this program, we have created a variable with the title “RollNumber” and have assigned to it a random number. Then, we have used the “printf” command to print this roll number on the terminal. Also, we have used the new line escape sequence again to print the output in a separate line.
The image shown below displays the output of the “printf” command:
Some More Examples of Printing Lines in the Scala Programming Language
To learn more about printing in the Scala programming language, you can review the following three examples:
Example # 1: Printing a List in the Scala Programming Language
In this example, we will show you how you can print all the elements of a Scala list in a single line. The Scala script shown below will demonstrate the process of printing the elements of a list on the terminal:
In this example, we have declared a list and populated it with some elements. Then, we have just used the “println” command with the name of our list to print all of its elements within a single line.
All the elements of our list are shown in the following image:
Example # 2: Printing a Set in the Scala Programming Language
This example will show you the process of printing the elements of a set in a single line. For doing that, you can use the Scala script shown below:
We have declared a Scala set and have assigned to it four different elements. Then, we have used the “println” command along with the name of this set to print its elements in a single line on the terminal.
The elements of our Scala set are shown in the following image:
Example # 3: Printing a Map in the Scala Programming Language
Finally, in this example, we will discuss the procedure of printing all the elements of a Scala map in a single line on the terminal. For that, we have used the Scala script shown below:
We have declared a Scala map and initialized it with three different key-value pairs. Then, we have used the “println” command with the name of this map to print all of its elements on the terminal in a single line.
The following image shows all the elements of our Scala map:
Conclusion
In this article, we wanted to talk about the different methods of printing a line in Scala in Ubuntu 20.04. Therefore, we first talked about the different methods of printing a line in the Scala programming language. After that, we shared some more examples with you in which you can print the elements of different data structures of this language on the terminal within a single line. By learning these methods, you can easily use these methods to print any of your desired lines on the terminal while using the Scala programming language.