Home
Scala
println Examples
This page was last reviewed on Dec 15, 2023.
Dot Net Perls
Println. In Scala 3.3 we often have console programs that perform computations. The programs must write data to the console—or their results will remain unknown.
Method notes. With print, println and printf we report to the screen. And with methods from scala.io.StdIn we read data from the console. We build interactive programs.
Println example. With the print methods we write data (like Strings or Ints) to the screen. Here we use 3 print functions—we do not need to access the Console type directly.
Start Println writes a value to the console and adds a trailing newline. We can pass Strings, Ints or other types to it.
Next Print() is the same as println but it adds no trailing newline. It just writes the data to the start of a line.
Finally Printf writes a format string and inserts additional arguments. It is similar to the Java String.format method.
object Program { def main(args: Array[String]): Unit = { // Use println on a string. println("Hello") // In Scala println is the same as Console.println. Console.println("World") // Use print to have no trailing newline. print("ABC") print(123) println() // Use printf with a format string. printf("Number = %d", 123) } }
Hello World ABC123 Number = 123
Readline. Here we get input from the user in the form of a string. The predefined readLine is deprecated, so we must access Scala.io.StdIn for a better version.
Here Control flow stops once readLine is invoked. Type something (like "cat") and press return. The variable now contains that string.
Warning This loop continues infinitely. In a real program we might want a "quit" command.
for, until
object Program { def main(args: Array[String]): Unit = { // Use an infinite loop. while (true) { // Read a line from the console window. val line = scala.io.StdIn.readLine() // Write the result string and a newline. printf("You typed: %s", line) println() } } }
cat You typed: cat bird You typed: bird
String interpolation. In Scala 3.3 we can use string interpolation syntax. We precede the string with lowercase "s" and then use the "$" symbol to refer to variable names.
object Program { def main(args: Array[String]): Unit = { val name = "birds" val color = "yellow" val amount = 12 // Use string interpolation syntax for output. println(s"The $amount $name are $color"); } }
The 12 birds are yellow
Standard library. The print and println functions are part of the predefined (Predef) object in Scala. These can be used anywhere, and are aliases to Console methods.
def print(x: Any) = Console.print(x) def println() = Console.println()
Deprecated warning. The readLine method is deprecated. Instead of using readLine we can call into scala.io.StdIn.readLine. This eliminates the warning.
warning: method readLine in trait DeprecatedPredef is deprecated: Use the method in scala.io.StdIn
Summary. For beginning Scala development, writing to and reading from the console is essential. With println and readLine most of this need is met.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 15, 2023 (new example).
Home
Changes
© 2007-2024 Sam Allen.