TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SCALA

Scala Console: println, printf and readLine

Use console functions like println and printf. Read lines from the console.
Console. A program does something. It computes the chance your investment will increase (or decrease) in value. We must write this value to the console—or it will remain unknown.
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.

Println: This writes a value to the console and adds a trailing newline. We can pass Strings, Ints or other types to it.

Print: This is the same as println but it adds no trailing newline. It just writes the data to the start of a line.

Printf: This writes a format string and inserts additional arguments. It is similar to the Java String.format method.

String.format: Java
Scala program that uses println, print and printf // 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) Output 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.

While: This loop continues infinitely. In a real program we might want a "quit" command.

While
Scala program that uses readLine, scala.io.StdIn // 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() } Output cat You typed: cat bird You typed: bird
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.Standard Library: scala-lang.org
Standard library definitions: 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 on readLine: warning: method readLine in trait DeprecatedPredef is deprecated: Use the method in scala.io.StdIn
A summary. For beginning Scala development, writing to and reading from the console is essential. With println and readLine most of this need is met.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf