×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

How to Print a Line in Scala?

By IncludeHelp Last updated : October 07, 2024

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. Using printf() Method
  2. Using print() Method
  3. Using println() Method

1) Using printf() Method

The 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.

Printing a line using printf() Method

Just use \n, where you want to print the line.

object MyClass {
  def main(args: Array[String]): Unit = {
    printf("Hello\nWorld!")
  }
}
Output
Hello
World!

2) Using print() Method

The print() method is just like printf() 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.

Printing a line using print() Method

Just use \n, where you want to print the line.

object MyClass {
  def main(args: Array[String]): Unit = {
    print("Hello\nWorld!")
  }
}
Output
Hello
World!

3) Using println() Method

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.

Printing a line using println() Method

There is no need to write \n to print the line, println() will add a new line after string to be printed. But, if you want to print a line within the given string - use \n.

object MyClass {
  def main(args: Array[String]): Unit = {
    println("Hello")
    println("World!")

    println("Welcome to the world of programming")
    println()
    println()
    println()
    println("Bye Bye!!!")
  }
}
Output
Hello
World!
Welcome to the world of programming



Bye Bye!!

Bonus Tip!

Print Line and Tab Space

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.

Scala code to print line and tab space

object MyClass {
  def main(args: Array[String]): Unit = {
    // "\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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.