Home » Scala language

How to print a line in Scala?

In this Scala tutorial, we will learn about all three methods to print a line is Scala along with sample code for each. These limitations and where you should and should not use them.
Submitted by Shivang Yadav, on July 04, 2019

Printing a line in Scala

To print a line in Scala, there are majorly three inbuilt methods that can help you print your string to the output string. You can use any of them in your code as they are inbuilt in Scala.

  1. printf()
  2. print()
  3. println()

1) printf()

printf() method is the old traditional C programming type method that outputs formatted strings to the output screen. The syntax for printing statements using printf() method is:

    var rlno = 324;
    printf("Roll Number =  %d", rlno)

This will print, "Roll Number = 324".

There are some limitations in this like does not provide a line break at the end of the statement, deals in traditional coding that is a bit tricky.

2) print()

The print() method in Scala is just like print() one, but the method of the passing string is a bit different. For example,

var rlno = 324;
print("Roll Number = " + rlno);

The output will be the same as it was with printf().

This method is a bit easy to write for the programmer and it does not require passing references. But this does not add a line break at the end of the line.

3) println()

The println() method is used to print a statement and add a line break to it at the end by default. All functioning is the same but your code will add a line break and the cursor will move to the next line. For example,

var rlno = 324;
println("Roll Number = " + rlno);

It will print the same output but will move the cursor to the next line of the output screen.

BONUS: How to add a line break or tab space in the output of your code?

If you need to format your output code to make it look good. You can use basic newline character (\n) or a tab character (\t) sequence to add some indentation in your code.

  • To add new line to your output screen, use \n
  • To add a tab space to your output screen, use \t

Example:

object MyClass {
        def main(args: Array[String]) {
            //"\n" example
            //it will print "Happy learning!"" in a new line
            println("Hello world\nHappy learning!") 
            
            //"\t" example
            //it will print "Happy learning!"" after a tab space
            println("Hello world\tHappy learning!") 
      }
   }

Output

Hello world
Happy learning!
Hello world	Happy learning!





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.