Open In App

Scala Console | println, printf and readLine

Console implements functions for displaying the stated values on the terminal i.e, with print, println, and printf we can post to the display. It is also utilized in reading values from the Console with the function from scala.io.StdIn. It is even helpful in constructing interactive programs. Let’s discuss it in detail and also lets see some examples related to it.




// Scala program of print
// functions
 
// Creating an object
object GfG
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Applying console with println
        Console.println("GeeksforGeeks")
 
        // Displays output with no
        // trailing lines
        print("CS")
        print("_portal")
 
        // Used for a newline
        println()
 
        // Displays format string
        printf("Age = %d", 24)
    }
}

Output:
GeeksfoGeeks
CS_portal
Age = 24




// Scala program of readLine()
// method
 
// Creating an object
object GfG
 
{
// Main method
def main(args: Array[String])
 
{
// Applying a loop
while (true) {
 
    // Reads the line from the Console
    val result = scala.io.StdIn.readLine()
 
    // Displays the string that is
    // given by the user
    printf("Enter the String: %s", result)
   
    //prints newline
    println()
     }
   }
}

Output:
//giving user defined inputs
GfG 
//output by readline
Enter the String: Gfg 
//again giving inputs
Nidhi 
//output
Enter the String: Nidhi 

Article Tags :